Rumah > Soal Jawab > teks badan
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");