这是发送给单人的工具类
public class EmailUtil {
public static int sendEmail(String sender,String recipient,
String emailSubject,String emailContent){
int res=0;
try {
Properties p = new Properties();
p.setProperty("mail.host","smtp.126.com");
p.setProperty("mail.smtp.auth", "true");
p.setProperty("mail.transport.protocol", "smtp");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
p.put("mail.smtp.ssl.enable", "true");
p.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(p, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication("idisStudio@126.com","idisstudio412");
return pa;
}
});
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(sender));
msg.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(recipient));
msg.setSubject(emailSubject);
msg.setContent(emailContent,"text/plain;charset=utf-8");
Transport.send(msg);
System.out.println("邮件发送成功");
res=1;
} catch (Exception e) {
System.out.println("邮件发送失败: "+e.getMessage());
res=0;
}
return res;
}
}
多种功能(转)
package com.test;
import java.util.Properties;
import javax.mail.Authenticator;
import javax.mail.Message.RecipientType;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import com.sun.mail.util.MailSSLSocketFactory;
public class JavaMailUtils {
public static int sendEmail(String mailServer,final String loginAccount,final String loginAuthCode,String sender,String[] recipients,
String emailSubject,String emailContent,String emailContentType){
int res=0;
try {
Properties p = new Properties();
p.setProperty("mail.host",mailServer);
p.setProperty("mail.smtp.auth", "true");
p.setProperty("mail.transport.protocol", "smtp");
MailSSLSocketFactory sf = new MailSSLSocketFactory();
sf.setTrustAllHosts(true);
p.put("mail.smtp.ssl.enable", "true");
p.put("mail.smtp.ssl.socketFactory", sf);
Session session = Session.getDefaultInstance(p, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
PasswordAuthentication pa = new PasswordAuthentication(loginAccount,loginAuthCode);
return pa;
}
});
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(sender));
InternetAddress[] receptientsEmail=new InternetAddress[recipients.length];
for(int i=0;i<recipients.length;i++){
receptientsEmail[i]=new InternetAddress(recipients[i]);
}
msg.setRecipients(RecipientType.TO,receptientsEmail);
msg.setSubject(emailSubject);
msg.setContent(emailContent,emailContentType);
Transport.send(msg);
System.out.println("邮件发送成功");
res=1;
} catch (Exception e) {
System.out.println("邮件发送失败: "+e.getMessage());
res=0;
}
return res;
}
public static void main(String[] args) throws Exception {
int res=sendEmail("smtp.qq.com", "这里输入你的qq邮箱", "这里输入前面说到的授权码", "发送人的qq邮箱", new String[]{
"1974544863@qq.com","linsenzhong@126.com"
}, "节日祝福", "祝你国庆节快乐,欢迎来我的blog: <a href='http://blog.csdn.net/u013871100'>我的blog</a>,祝您生活愉快!","text/html;charset=utf-8");
System.out.println("\n发送结果:"+res);
}
}