SMTP邮件发送,需要一个SMTP邮件服务器。
1.
验证发件人的授权
新建java类,MyAuthenticator.java 内容如下:
import javax.mail.PasswordAuthentication;
class MyAuthenticator extends javax.mail.Authenticator
{
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password)
{
this.strUser = user;
this.strPwd = password;
}
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(strUser, strPwd);
}
}
2.SMTP邮件发送
新建java类,SendEmail.java 内存如下:
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.Multipart;
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;
public class SendEmail
{
private static String host = ""; // SMTP服务器地址
private static String password = ""; // 密码
private static String from = ""; // 发件人
private static String to = ""; // 收件人
private static String cc = “”; // 抄送人(中间使用;隔开)
public static void send(String subject, String content, String filename)
{
Properties props = System.getProperties();
props.put("mail.smtp.host", host);//指定SMTP服务器
props.put("mail.transpost.protocol", "smtp");
props.put("mail.smtp.port", "25");
props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
//需要SSL安全验证
props.setProperty("mail.smtp.starttls.enable", "true");
// props.setProperty("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
// props.setProperty("mail.smtp.socketFactory.fallback", "false");
// props.setProperty("mail.smtp.socketFactory.port", "25");
try
{
MyAuthenticator myauth = new MyAuthenticator(from, password);
Session mailSession = Session.getDefaultInstance(props, myauth);
mailSession.setDebug(true);//是否在控制台显示debug信息
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));//发件人
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));//收件人
// 是否有抄送人
if (!StringUtil.isBlank(cc))
{
String[] bccs = cc.split(";");
InternetAddress bcc = null;
InternetAddress[] bccAddrs = new InternetAddress[bccs.length];
for (int i = 0; i < bccs.length; i++)
{
if (!"".equals(bccs[i].trim()))
{
bcc = new InternetAddress(bccs[i].trim());
bccAddrs[i] = bcc;
}
}
message.setRecipients(Message.RecipientType.CC, bccAddrs);
}
message.setSubject(subject);//邮件主题
// 有附件
if (!StringUtil.isBlank(filename))
{
Multipart mp = new MimeMultipart();
BodyPart bp = new MimeBodyPart();
bp.setContent(content, "text/html;charset=utf-8");//邮件正文
mp.addBodyPart(bp);
File file = new File(filename);
if (file.exists())
{
BodyPart mdp = new MimeBodyPart();
FileDataSource fileds = new FileDataSource(file);
mdp.setDataHandler(new DataHandler(fileds));
mdp.setFileName(fileds.getName());
mp.addBodyPart(mdp);
}
message.setContent(mp);
}
// 无附件
else
{
message.setContent(content, "text/html;charset=utf-8");//邮件正文
}
message.saveChanges();
Transport.send(message);
}
catch (Exception e)
{
return;
}
}
public static void main(String[] args)
{
SendEmail.send("测试邮件", "邮件发送测试!", "本地附件地址");
}
posted on 2011-04-19 10:42
Jarry 阅读(237)
评论(0) 编辑 收藏 所属分类:
Java