不带附件的发送邮件

package kpi.mail;


/*
* 创建日期 2005-7-14
*
* 更改所生成文件模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;


/**
* @author jyliu
*
* 更改所生成类型注释的模板为
* 窗口 > 首选项 > Java > 代码生成 > 代码和注释
*/
public class SendWithSmtp {
String host = "211.157.10.8";

String user = "mayx";//

String password = "999999";

public void setHost(String host) {
this.host = host;
}

public void setAccount(String user, String password) {
this.user = user;
this.password = password;
}

public void send(String from, String to, String subject, String content) {
Properties props = new Properties();
props.put("mail.smtp.host", host);//指定SMTP服务器211.157.10.8
props.put("mail.smtp.auth", "true");//指定是否需要SMTP验证
try {
Session mailSession =Session.getDefaultInstance(props,new OK_Authenticator(user, password));
// mailSession.setDebug(true); //是否在控制台显示debug信息
Transport transport = mailSession.getTransport("smtp");


mailSession.setDebug(true);//是否在控制台显示debug信息

//发送不带附件的邮件
Message message = new MimeMessage(mailSession);
message.setFrom(new InternetAddress(from));//发件人
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));//收件人
message.setSubject(subject);//邮件主题  subject 自己创建
content = collectContent(content);  // content自己创建
message.setDataHandler(new DataHandler(new ByteArrayDataSource(content, "text/html")));
message.setText(content);//邮件内容 
message.saveChanges();
transport.connect(host, user, password);
transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
// transport.sendMessage(message, message.getAllRecipients());
transport.close();
} catch (Exception e) {
System.out.println(e);
}
}

public String collectContent(String in)
throws  Exception {
StringBuffer sb = new StringBuffer();
sb.append("\n");
sb.append("绩效相关信息\n");
sb.append("\n");
//sb.append("<meta http-equiv=" + "Content-Type" + "content=" + "text/html; charset=gb2312"+ ">");
sb.append("\n");
sb.append("\n");
sb.append(in+"\n");
return sb.toString();
}


public static void main(String args[]) throws Exception {
// SendWithSmtp sm = new SendWithSmtp();
// sm.setHost("211.157.10.8");//指定要使用的邮件服务器
// sm.setAccount("mayx","999999");//指定帐号和密码
// /*
// * @param String 发件人的地址 @param String 收件人地址 @param String 邮件标题 @param
// * String 邮件正文
// */
// //[email protected]
// sm.send("[email protected]", "[email protected]", "111","1111");
}


class OK_Authenticator extends Authenticator {
String username, password;

public OK_Authenticator(String username, String password) {
super();
this.username = username;
this.password = password;
}

public javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(username, password);
}
}
}


后台action的代码
else if("1".equals(type)){
String[] sbEmails = sbEmail.split(";");
SendWithSmtp sw = new SendWithSmtp();
sw.setHost("211.157.10.8");//指定要使用的邮件服务器
sw.setAccount("mayx","999999");//指定帐号和密码
for(int i=0 ; i<sbEmails.length;i++){
sbEmails[i] = sbEmails[i].replaceAll("<", "");
sbEmails[i] = sbEmails[i].replaceAll(">", "");
//System.out.println("***************sbEmails[i]:"+sbEmails[i]);
sw.send("[email protected]", sbEmails[i], title,content);
}

你可能感兴趣的:(qq)