import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Email {
public static void main(String[] args) {
InternetAddress[] popAddressList = null;
String smtpServer = "smtp.qq.com";
String popServer = "pop.qq.com";
String SmtpAddress = "2668208858@qq.com";
String PopAddresslist = "545739504@qq.com";
String Subject = "这是一封测试邮件";
String Type = "text/html";
String messageText = "邮件的内容:hello,world";
String[] arrArchievList = new String[3];
arrArchievList[0] = "c:\\1.JPG";
arrArchievList[1] = "c:\\2.JPG";
arrArchievList[2] = "c:\\3.jpg";
boolean sessionDebug = false;
try
{
java.util.Properties props = System.getProperties();
props.put("mail.smtp.host",smtpServer);//存储发送邮件服务器的信息
props.put("mail.smtp.auth","false");//同时通过验证
props.put("mail.transport.protocol","smtp");
Session mailSession = Session.getInstance(props);//根据属性新建一个邮件会话
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
// 设定发件人的地址
msg.setFrom(new InternetAddress(SmtpAddress));
// 设定收信人的地址
popAddressList = InternetAddress.parse(PopAddresslist,false);
msg.setRecipients(Message.RecipientType.TO, popAddressList);
// 设定信中的主题
msg.setSubject(Subject);
// 设定送信的时间
msg.setSentDate(new Date());
// 是否以附件方式发送邮件
boolean bolSendByArchieve = false;
// 如果有附件,先将由件内容部分存起来
if (arrArchievList != null && arrArchievList.length >0)
{
// 1.保存内容
MimeMultipart mp = new MimeMultipart();
MimeBodyPart mailContentPart = new MimeBodyPart();
mailContentPart.setContent(messageText,Type + ";charset=GBK");
msg.setContent(messageText,Type + ";charset=GBK");
// 这句很重要,千万不要忘了
mp.setSubType("related");
mp.addBodyPart(mailContentPart);
// 2.保存多个附件
for (int index = 0;index < arrArchievList.length;index++)
{
File file = new File(arrArchievList[index]);
MimeBodyPart mailArchieve = new MimeBodyPart();
FileDataSource fds = new FileDataSource(arrArchievList[index]);
mailArchieve.setDataHandler(new DataHandler(fds));
mailArchieve.setFileName(MimeUtility.encodeText(fds.getName(),"GBK","B"));
mp.addBodyPart(mailArchieve);
}
// 3.最后集成内容和附件,一起发送
msg.setContent(mp);
}
else
{
msg.setContent(messageText,Type + ";charset=GBK");
}
//发送邮件
Transport transport;
msg.saveChanges();//存储邮件信息
transport = mailSession.getTransport("smtp");
//以smtp方式登录邮箱 username填写你的发送邮件的用户名如bluewens,userpwd填写你的密码,如获88888888,即transport.connect("smtp.163.com","bluewens","88888888");
transport.connect("smtp.qq.com","2668208858@qq.com");
transport.sendMessage(msg,msg.getAllRecipients());//发送邮件,其中第二个参数是所有
//已设好的收件人地址
props.put("pop.qq.com", "false");
transport.close();
System.out.println("邮件已发送成功!");
}
catch(Exception ex)
{
ex.printStackTrace();
}
}
The SMTP server used is incorrect. smtp.qq.com is for Tencent users to log in. To send emails directly without logging in, you should use the MX record of qq.com:
That is, mx1.qq.com.
In addition, if you declare that the sender is Tencent’s own account, it is likely to be rejected by the server (because it knows that you are not it). Even if you declare the sender to be from another domain, it is very likely to be treated as spam, especially if you don't even have A and MX records.
It is safer to use SMTP authorization to log in before sending, and you don’t have to judge which server to connect to.
transport.connect("smtp.qq.com","2668208858@qq.com"); is to change this code to:
transport.connect("mx1.qq.com","2668208858@qq.com");