java邮件发送工具_java实现发送邮件工具

package com.wxjiameng.utils;

import java.util.Date;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.Address;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

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;

import javax.mail.internet.MimeUtility;

import org.apache.log4j.Logger;

/**

*

*

* @类编号:

* @类名称:EmailsUtils

* @内容摘要: 发送邮件

*@author:鹿伟伟

* @创建日期:2016年4月25日 下午6:06:15

* @修改人:

* @修改日期:

* @修改描述:简单描述修改的内容

*@version 1.0.0

*

*/

public class EmailsUtils {

public static Logger logger = Logger.getLogger(EmailsUtils.class);

//Multipart对象,邮件内容,标题,附件等内容均添加到其中后再生成MimeMessage对象

private Multipart mp = new MimeMultipart();

private final String CHAR_SET = "UTF-8";

/**

*

*@Description: 发送邮件

*@param EmailParaForm 发件人等发送邮件参数

*@return

*@author: YuPeng

*@date 2012-5-25 下午5:13:36

*/

public boolean SendEmails(EmailParaForm emailParaForm){

boolean isSucc = false;

if (emailParaForm == null){

return isSucc;

}

try{

//JavaMail需要Properties来创建一个session对象。它将寻找字符串"mail.smtp.host",属性值就是发送邮件的主机.

//Properties对象获取诸如邮件服务器、用户名、密码等信息,以及其他可在整个应用程序中 共享的信息。

Properties props=new Properties();//也可用Properties props = System.getProperties();

props.put("mail.smtp.host",emailParaForm.getStmpHost());//存储发送邮件服务器的信息

props.put("mail.smtp.port", "" + emailParaForm.getTport()); // 端口号

props.put("mail.transport.protocol",emailParaForm.getProtocol());

props.put("mail.smtp.auth",emailParaForm.getNeedAuth());//同时通过验证

if (null != emailParaForm.getAgentIp() && emailParaForm.getAgentIp().length() != 0 && !emailParaForm.getAgentIp().equals("0")) {

props.put("mail.agentIp", emailParaForm.getAgentIp());

}

if (null != emailParaForm.getAgentPort() && emailParaForm.getAgentPort().length() != 0 && !emailParaForm.getAgentPort().equals("0")) {

props.put("mail.agentPort", emailParaForm.getAgentPort());

}

//方法二:(如果是在weblogin配置JavaMail:则需指定JNDI名检索

//Context ctx=new InitialContext();

//Session s=(Session)ctx.lookup("MailSession");

//Message msg=new MimeMessage(s);

//这个Session类代表JavaMail 中的一个邮件session. 每一个基于 JavaMail的应用程序至少有一个session但是可以有任意多的session。

//Session类定义全局和每个用户的与邮件相关的属性。这此属性说明了客房机和服务器如何交流信息。

//Authenticator也是一个抽象类,你需要自己编写子类已备应用。

Session s = Session.getInstance(props,null);//根据属性新建一个邮件会话,null参数是一种Authenticator(验证程序) 对象

String isDebug = emailParaForm.getIsDebug();

if (isDebug != null && isDebug.trim().equalsIgnoreCase("true")){

s.setDebug(true);//设置调试标志,要查看经过邮件服务器邮件命令,可以用该方法

}

// 一旦创建了自己的Session对象,就是该去创建要发送的消息的 时候了。这时就要用到消息类型(MimeMessage是其中一种类型)。

// Message对象将存储我们实际发送的电子邮件信息,Message对象被作为一个MimeMessage对象来创建并且需要知道应当选择哪一个JavaMail session。

// Message类表示单个邮件消息,它的属性包括类型,地址信息和所定义的目录结构。

Message message=new MimeMessage(s);//由邮件会话新建一个消息对象

//message.setContent("hello","test/plain");//设置消息的内容类型,如果发送的格式有HTML格式就必须设置,

//message.setText("Hello");//发送一般文本格式的消息

//设置邮件,一旦您创建了 Session 和 Message,并将内容填入消息后,就可以用Address确定信件地址了。

//如果想让一个名字出现在电子邮件地址后,也可以将其传递给构造器:

//Address from=new InternetAddress("xmqds@21cn.com","qdison");//发件人的邮件地址

Address from=new InternetAddress(emailParaForm.getTfrom());//发件人的邮件地址

message.setFrom(from);//设置发件人

Address to=new InternetAddress(emailParaForm.getTto());//收件人的邮件地址

message.setRecipient(Message.RecipientType.TO,to);//设置收件人,并设置其接收类型为TO,还有3种预定义类型如下:

//Message.RecipientType.TO  收件人

//Message.RecipientType.CC  抄送

//Message.RecipientType.BCC  暗送

message.setSubject(emailParaForm.getTtitle());//设置主题

message.setSentDate(new Date());//设置发信时间

setBody(emailParaForm.getTcontent());

//String fileAttachment = "C:\\测试\\中国.txt";

//添加附件

String fileAttachments = emailParaForm.getFileAttachments();

if(null!=fileAttachments){

String[] fileAttArr = fileAttachments.split(";");

for (int i = 0; i < fileAttArr.length; i++){

String fileAtt = fileAttArr[i];

addFileAffix(fileAtt);

}

}

message.setContent(mp);

message.saveChanges();//存储邮件信息

// Transport 是用来发送信息的,

// 用于邮件的收发打操作。

Transport transport=s.getTransport(emailParaForm.getProtocol());

transport.connect(emailParaForm.getStmpHost(),emailParaForm.getUserName(),emailParaForm.getPassword());//以smtp方式登录邮箱

transport.sendMessage(message,message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址

transport.close();

isSucc = true;;

} catch(MessagingException e){

logger.error(e);

e.printStackTrace();

}

return isSucc;

}

/**

* 邮件添加附件

*

*@param filePath 包括路径名的完整本地文件名

*@return boolean

*/

public boolean addFileAffix(String filePath) {

try {

BodyPart bp = new MimeBodyPart();

FileDataSource fileds = new FileDataSource(filePath);

bp.setDataHandler(new DataHandler(fileds));

String fileNm = fileds.getName();

bp.setFileName(MimeUtility.encodeText(fileNm));

mp.addBodyPart(bp);

return true;

}

catch (Exception e) {

logger.error(e);

System.err.println("Affix" + filePath + "accour error" + e);

return false;

}

}

/**

* 设置邮件内容

*

*@param body 邮件内容

*@return boolean

*/

public boolean setBody(String body) {

try {

BodyPart bp = new MimeBodyPart();

bp.setContent("" + body, "text/html;charset=" + CHAR_SET);

mp.addBodyPart(bp);

return true;

} catch (Exception e) {

logger.error(e);

System.err.println("set mail body is error" + e);

return false;

}

}

/**

* 发送邮件

*@param receiveUser 接收人

*@param title 标题

*@param content 内容

* 返回true则发送成功,false发送失败

*@return

*/

public boolean send(String receiveUser,String title,String content){

boolean isSucc = false;

EmailParaForm spForm = new EmailParaForm();

spForm.setId("FFFFFF");

spForm.setTfrom("***@163.com"); // 发出者邮箱

spForm.setStmpHost("smtp.163.com"); // 邮箱类型

spForm.setNeedAuth("true");//是否需要认证

spForm.setIsDebug("false"); //调试

spForm.setProtocol("smtp"); //协议

spForm.setUserName("***@163.com");//用户名

spForm.setPassword("****");//密码

spForm.setTport("25");//端口。默认发出

spForm.setTto(receiveUser);

spForm.setTcontent(content);

spForm.setTtitle(title);

String formPass = spForm.getPassword();

if (formPass != null ){

isSucc=SendEmails(spForm);

}

return isSucc;

}

public static void main(String[] args){

EmailsUtils e = new EmailsUtils();

e.send("**@qq.com", "维基百科", "自由的百科全书:维基百科是一个内容自由、任何人都能参与、并有多种语言的百科全书协作计划...");

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
/* * JCatalog Project */ package com.hexiang.utils; import java.util.List; import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.Properties; import javax.mail.Session; import javax.mail.Transport; import javax.mail.Message; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import com.hexiang.exception.CatalogException; /** * Utility class to send email. * * @author <a href="380595305@qq.com">hexiang</a> */ public class EmailUtil { //the logger for this class private static Log logger = LogFactory.getLog("com.hexiang.util.EmailUtil"); /** * Send email to a single recipient. * * @param smtpHost the SMTP email server address * @param senderAddress the sender email address * @param senderName the sender name * @param receiverAddress the recipient email address * @param sub the subject of the email * @param msg the message content of the email */ public static void sendEmail(String smtpHost, String senderAddress, String senderName, String receiverAddress, String sub, String msg) throws CatalogException { List<String> recipients = new ArrayList<String>(); recipients.add(receiverAddress); sendEmail(smtpHost, senderAddress, senderName, recipients, sub, msg); } /** * Send email to a list of recipients. * * @param smtpHost the SMTP email server address * @param senderAddress the sender email address * @param senderName the sender name * @param recipients a list of receipients email addresses * @param sub the subject of the email * @param msg the message content of the email */ public static void sendEmail(String smtpHost, String senderAddress, String senderName, List<String> recipients, String sub, String msg) throws CatalogException { if (smtpHost == null) { String errMsg = "Could not send email: smtp host address is null"; logger.error(errMsg); throw new CatalogException(errMsg); } try { Properties props = System.getProperties(); props.put("mail.smtp.host", smtpHost); Session session = Session.getDefaultInstance(props, null ); MimeMessage message = new MimeMessage( session ); message.addHeader("Content-type", "text/plain"); message.setSubject(sub); message.setFrom(new InternetAddress(senderAddress, senderName)); for (Iterator<String> it = recipients.iterator(); it.hasNext();) { String email = (String)it.next(); message.addRecipients(Message.RecipientType.TO, email); } message.setText(msg); message.setSentDate( new Date() ); Transport.send(message); } catch (Exception e) { String errorMsg = "Could not send email"; logger.error(errorMsg, e); throw new CatalogException("errorMsg", e); } } }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值