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();
}
}
PHP中文网2017-04-17 11:31:51
使用的 SMTP 伺服器不對。 smtp.qq.com 是給騰訊使用者登入用的,不登入直接寄信應該用 qq.com 的 MX 記錄:
$ host -t MX qq.com
qq.com mail is handled by 20 mx2.qq.com.
qq.com mail is handled by 30 mx1.qq.com.
qq.com mail is handled by 10 mx3.qq.com.
即 mx1.qq.com。
另外,如果聲明寄件人為騰訊自己的帳號的話,很有可能被伺服器拒絕(因為它知道你不是它)。即使聲明寄件者為其它域,也極有可能被當作垃圾郵件對待,特別是當你連 A 和 MX 記錄都沒有的時候。
還是使用 SMTP 授權登入之後再發送穩健一些,也不用自己判斷該連哪個伺服器。
PHP中文网2017-04-17 11:31:51
transport.connect("smtp.qq.com","2668208858@qq.com");是將這段程式碼改成:
transport.connect("mx1.qq.com","2668208858@qq.com");