程序来自各类网络教程
Jar来自CSDN

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
//这是仅在控制台读入一行邮件正文的程序
//注意:程序中的密码为明文!
import java.io.*;
import java.util.Properties;
import java.util.Scanner;
import javax.mail.Address;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class TestMail
{
public static void main(String[] args) throws Exception
{
Scanner scan=new Scanner(System.in);
String content=scan.nextLine();
Properties props = new Properties();
//SMTP服务器请自行修改
props.put("mail.smtp.host", "smtp.sina.com");
props.put("mail.smtp.auth", "true");
Session session = Session.getInstance(props);
MimeMessage message = new MimeMessage(session);
Address address = new InternetAddress("Sample@sina.com");
message.setFrom(address);
Address toAddress = new InternetAddress("987654321@qq.com");
message.setRecipient(MimeMessage.RecipientType.TO, toAddress);
//主题
message.setSubject("DEFAULT");
//正文
message.setText(content);
message.saveChanges();
session.setDebug(true);
Transport transport = session.getTransport("smtp");
transport.connect("smtp.sina.com", "Sample@sina.com", "SamplePwd");
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}