package com.agenda.tdyhweb.util.email;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;public class EmailAuthenticator extends javax.mail.Authenticator {
private String userName = null;private String password = null;
public void setUserName(String aUserName) {
this.userName = aUserName;
}public void setPassword(String aPassword) {
this.password = aPassword;
}public EmailAuthenticator() {
super();
}public EmailAuthenticator(String aUserName, String aPassword) {
super();
setUserName(aUserName);
setPassword(aPassword);
}// 一定要有这个方法,它是在需要身份验证时自动被调用的
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(this.userName, this.password);
}
}---------------------------
package com.agenda.tdyhweb.util.email;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Session;
import javax.mail.SendFailedException;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMultipart;
import javax.mail.internet.MimeUtility;
import javax.activation.FileDataSource;
import javax.activation.DataHandler;
import java.util.Date;
import java.util.Properties;
import java.io.File;
import java.io.UnsupportedEncodingException;public class EmailSender {
private Session session = null;private String mailFrom = null;
private String mailTo = null;
private String mailCc = null;
private String mailBcc = null;
private String subject = null;
private String content = null;
private String attachFile = null;
private String mailHost = null;
private String userName = null;
private String password = null;
public EmailSender(String mailHost) {
Properties prop = new Properties();
prop.put("mail.transport.protocol", "smtp");
this.session = Session.getDefaultInstance(prop, null);
}public EmailSender(String mailHost, String userName, String password) {
Properties prop = new Properties();
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.host", mailHost);
this.session = Session.getInstance(prop, new EmailAuthenticator(
userName, password));
// this.session.setDebug(true);
}public EmailSender(String mailHost, String proxyHost, int proxyPort) {
Properties prop = new Properties();
prop.put("http.proxySet", "true");
prop.put("http.proxyHost", proxyHost);
prop.put("http.proxyPort", String.valueOf(proxyPort));
prop.put("mail.transport.protocol", "smtp");
this.session = Session.getDefaultInstance(prop, null);
}public EmailSender(String mailHost, String userName, String password,
String proxyHost, int proxyPort) {
Properties prop = new Properties();
prop.put("http.proxySet", "true");
prop.put("http.proxyHost", proxyHost);
prop.put("http.proxyPort", String.valueOf(proxyPort));
prop.put("mail.transport.protocol", "smtp");
prop.put("mail.smtp.host", mailHost);
this.session = Session.getDefaultInstance(prop, new EmailAuthenticator(
userName, password));
}public void sendSimpleMail() throws MessagingException, SendFailedException {
if (this.mailFrom == null) {
throw new RuntimeException("NO MAIL FROM");
}
if (this.mailTo == null) {
throw new RuntimeException("NO MAIL TO");
mimeMsg.setFrom(new InternetAddress(mailFrom));
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
if (mailCc != null) {
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress
.parse(mailCc));
if (mailBcc != null) {
mimeMsg.setRecipients(Message.RecipientType.BCC, InternetAddress
.parse(mailBcc));
mimeMsg.setSubject(subject, "GB2312");
mimeMsg.setText(content, "GB2312");
Transport.send(mimeMsg);
}// 带附件
public void sendMultiPartMail() throws MessagingException,
SendFailedException, UnsupportedEncodingException {
if (this.mailFrom == null) {
throw new RuntimeException("NO MAIL FROM");
}
if (this.mailTo == null) {
throw new RuntimeException("NO MAIL TO");
}
if (this.attachFile == null) {
throw new RuntimeException("NO MAIL TO");
}
File file = new File(attachFile);
if (!file.canRead()) {
throw new RuntimeException("Can't find the attached file!");
mimeMsg.setFrom(new InternetAddress(mailFrom));
mimeMsg.setRecipients(Message.RecipientType.TO, InternetAddress
if (mailCc != null) {
mimeMsg.setRecipients(Message.RecipientType.CC, InternetAddress
.parse(mailCc));
if (mailBcc != null) {
mimeMsg.setRecipients(Message.RecipientType.BCC, InternetAddress
.parse(mailBcc));
MimeBodyPart mbp1 = new MimeBodyPart();
//mbp1.setText(content, "GB2312");
mbp1.setContent(content.toString(),"text/html;charset=GB2312");
MimeBodyPart mbp2 = new MimeBodyPart();
FileDataSource fds = new FileDataSource(this.attachFile);
mbp2.setDataHandler(new DataHandler(fds));
MimeMultipart mp = new MimeMultipart();
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
// 设置邮件内容,将邮件body部分转化为HTML格式
}
public void setMailFrom(String mailFrom) {
this.mailFrom = mailFrom;
}public void setMailTo(String mailTo) {
this.mailTo = mailTo;
}public void setMailCc(String mailCc) {
this.mailCc = mailCc;
}public void setMailBcc(String mailBcc) {
this.mailBcc = mailBcc;
}public void setSubject(String subject) {
this.subject = subject;
}public void setContent(String content) {
this.content = content;
}public void setAttachFile(String attachFile) {
this.attachFile = attachFile;
}
}
---------------------------------------package com.agenda.tdyhweb.util.email;
import java.util.Locale;
import java.util.ResourceBundle; public class GetProperties {
private ResourceBundle bundle = null;
private String proName = "";
public static final String FILEPATH = "UserFiles/files/";
public static final String IMAGESPATH = "UserFiles/images/";
public GetProperties(String proname){
this.proName = proname;
} public String getProp(String type) throws Exception {
if (bundle == null) {
try {
bundle = ResourceBundle.getBundle(this.proName, Locale.getDefault());
} catch (Exception e) {
System.out.println("获取资源出错!" + e);
}
}
return bundle.getString(type);
}
}
-------------------------------------package com.agenda.tdyhweb.util.email;
import com.agenda.tdyhweb.util.email.GetProperties;;
public class Sender {
public Sender() {
}public void send(String to) {
try {
GetProperties gp = new GetProperties("email");
String host="";
String user="";
String pass="";
String from="";
String address="";
String title="理财专刊";
String content="尊敬的投资者:
";
content=content + " 您好!感谢您对支持与关注!
";
content=content + " 混合型基金将于8月27日拆分,并限量申购,为了让您更好地了";
content=content + "解此只基金,我们特制作了《理财专刊》供您参考,请参见附件.
";
content=content + " 选基金是股票配置比例为50%-70%的积极进取混合型基金,本基金";
content=content + "股市债市两手抓,在严格控制风险的同时更积极把握市场上升的机遇。在选择企业时,";
content=content + "本基金将更关注如新能源利用等符合“十一五规划”的题材,重点投资于较高的投资效";
content=content + "率下持续成长的公司,为基金持有人提供可持续的价值增值。
";
content=content + " 网上交易现已全面支持建行卡、农行卡、兴业银行卡,您持以上银行卡可";
content=content + "直接登录网站(办理开户、申购等手续,网上申购免排队,费率0.6%。
";
content=content + " 财智分享,合赢人生!
";
content=content + " 基金管理有限公司
";
host=gp.getProp("mail.host");
user=gp.getProp("mail.user");
pass=gp.getProp("mail.password");
from=gp.getProp("mail.defaultemail");
address=gp.getProp("address");
//title=gp.getProp("mail.title");
//content=gp.getProp("mail.content");
System.out.println(address);
EmailSender sender = new EmailSender(host,user,pass);
sender.setMailFrom(from);
sender.setMailTo(to);
sender.setSubject(title);
sender.setContent(content);
String filename=address + "\\zk.pdf";
System.out.println("filename is" + filename);
sender.setAttachFile(filename);
System.out.println("Sending email .....");
sender.sendMultiPartMail();
System.out.println("success!");
} catch (Exception e) {
e.printStackTrace();
}
}
}-----------------------------------------
package com.agenda.tdyhweb.util.email;
public class StringDataSource implements javax.activation.DataSource
{
private java.lang.String data;
public StringDataSource(java.lang.String data,java.lang.String type)
{
this.data = data;
this.type=type;
public java.io.InputStream getInputStream() throws java.io.IOException
{
return new java.io.StringBufferInputStream(data);
public java.io.OutputStream getOutputStream() throws java.io.IOException
{
throw new java.io.IOException("it does not support this method now!");
public java.lang.String getContentType(){
return type;
public java.lang.String getName()
{
return " mymail ";
}
}-----------------------------------------------------------
mail.basePath=http://www.beijing-hyundai.com.cn
#smtp
mail.host=21*.10*.30.21*
#??
mail.user=pin.huan
#??
mail.password=huaping
#????smtp??
mail.issmtp=true
#???
mail.defaultemail=pin.huan@cn.agenda-asia.com#??
mail.title="????"#??
mail.content="??"
address=e:
java发送邮件,可带附件
原创
©著作权归作者所有:来自51CTO博客作者Bensantan的原创作品,请联系作者获取转载授权,否则将追究法律责任
下一篇:关于java内存的理解
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Python 发送邮件 和 发送带附件邮件
平时运行一些脚本,需要把结果发送到邮箱,可以用python来处理,如下:1,有邮
sendmail mail python 邮件服务器 发送邮件