Last active
July 10, 2017 08:28
-
-
Save JunqiangYang/1f7895da69fa490e29f9ab1d940e6b57 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.ByteArrayInputStream; | |
import java.io.InputStream; | |
import java.util.Properties; | |
import javax.activation.DataHandler; | |
import javax.activation.DataSource; | |
import javax.activation.FileDataSource; | |
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.commons.mail.ByteArrayDataSource; | |
import com.nyonline.util.AppConfig; | |
import com.nyonline.util.BizLogger; | |
public class EmailCli { | |
public static String senderName() { | |
return AppConfig.get( "sms.mail.sender.name", ""); | |
} | |
public static String senderUserName() { | |
return AppConfig.get( "sms.mail.sender.username", ""); | |
} | |
private static Transport transport = null; | |
public static Session getSession() { | |
Properties properties = new Properties(); | |
properties.setProperty( "mail.smtp.host", AppConfig.get( "sms.mail.smtp.host", "") ); | |
properties.setProperty( "mail.smtp.auth", "true"); | |
properties.setProperty( "mail.sender.username", AppConfig.get( "sms.mail.sender.username", "") ); | |
properties.setProperty( "mail.sender.password", AppConfig.get( "sms.mail.sender.password", "") ); | |
Session session = Session.getInstance(properties); | |
session.setDebug(false); | |
return session; | |
} | |
public static Transport getTransport() { | |
if(transport==null) | |
{ | |
try { | |
transport = getSession().getTransport("smtp"); | |
transport.connect( AppConfig.get( "sms.mail.smtp.host", ""), | |
AppConfig.get( "sms.mail.sender.username", ""), | |
AppConfig.get( "sms.mail.sender.password", "") ); | |
//transport.close(); | |
} catch ( Exception e ) { | |
BizLogger.error( e, "EmailCli getTransport fail" ); | |
} | |
} | |
return transport; | |
} | |
/** | |
* 发送邮件 | |
* | |
* @param subject | |
* 邮件主题 | |
* @param sendHtml | |
* 邮件内容 | |
* @param receiveUser | |
* 收件人地址 | |
*/ | |
public static int doSendHtmlEmail(String subject, String sendHtml, | |
String receiveUser) { | |
Transport transport=null; | |
try { | |
Session session = getSession(); | |
MimeMessage message = new MimeMessage(session); | |
// 发件人 | |
InternetAddress from = new InternetAddress( MimeUtility.encodeWord( senderName() )+" <"+senderUserName()+">"); | |
message.setFrom(from); | |
// 收件人 | |
InternetAddress to = new InternetAddress(receiveUser); | |
message.setRecipient(Message.RecipientType.TO, to);//还可以有CC、BCC | |
// 邮件主题 | |
message.setSubject(subject); | |
String content = sendHtml.toString(); | |
// 邮件内容,也可以使纯文本"text/plain" | |
message.setContent(content, "text/html;charset=UTF-8"); | |
// 保存邮件 | |
message.saveChanges(); | |
getTransport().sendMessage(message, message.getAllRecipients()); | |
} catch (Exception e) { | |
//e.printStackTrace(); | |
try { | |
transport.close(); | |
} catch (MessagingException e1) { | |
} | |
transport=null; | |
return -1; | |
} | |
return 0; | |
} | |
/** | |
* 发送带附件邮件 | |
* | |
* @param subject | |
* 邮件主题 | |
* @param sendHtml | |
* 邮件内容 | |
* @param receiveUser | |
* 收件人地址 | |
*/ | |
public static int doSendAffixEmail(String subject, String sendHtml, | |
String receiveUser, String affix) { | |
Transport transport=null; | |
try | |
{ | |
Session session = getSession(); | |
MimeMessage message = new MimeMessage(session); | |
//加载发件人地址 | |
InternetAddress from = new InternetAddress( MimeUtility.encodeWord( senderName() )+" <"+ senderUserName() +">"); | |
message.setFrom( from ); | |
//加载收件人地址 | |
message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveUser)); | |
//加载标题 | |
message.setSubject(subject); | |
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件 | |
Multipart multipart = new MimeMultipart(); | |
//设置邮件的文本内容 | |
BodyPart contentPart = new MimeBodyPart(); | |
contentPart.setText( sendHtml.toString() ); | |
multipart.addBodyPart(contentPart); | |
//添加附件 | |
BodyPart messageBodyPart= new MimeBodyPart(); | |
DataSource source = new FileDataSource(affix); | |
//添加附件的内容 | |
messageBodyPart.setDataHandler(new DataHandler(source)); | |
//添加附件的标题 | |
messageBodyPart.setFileName( MimeUtility.encodeWord( source.getName() ) ); | |
multipart.addBodyPart(messageBodyPart); | |
//将multipart对象放到message中 | |
message.setContent(multipart); | |
//保存邮件 | |
message.saveChanges(); | |
getTransport().sendMessage(message, message.getAllRecipients()); | |
}catch(Exception e){ | |
try { | |
transport.close(); | |
} catch (MessagingException e1) { | |
} | |
transport=null; | |
return -1; | |
} | |
return 0; | |
} | |
/** | |
* 发送带附件邮件 | |
* | |
* @param subject | |
* 邮件主题 | |
* @param sendHtml | |
* 邮件内容 | |
* @param receiveUser | |
* 收件人地址 | |
* @param affixName附件名 | |
* @param affix 附件字节流 | |
*/ | |
public static int doSendAffixEmail2(String subject, String sendHtml, String receiveUser, | |
String affixName, byte[] affix) { | |
try{ | |
Session session = getSession(); | |
MimeMessage message = new MimeMessage(session); | |
//加载发件人地址 | |
InternetAddress from = new InternetAddress( MimeUtility.encodeWord( senderName() )+" <"+ senderUserName() +">"); | |
message.setFrom( from ); | |
//加载收件人地址 | |
message.addRecipient(Message.RecipientType.TO, new InternetAddress(receiveUser)); | |
//加载标题 | |
message.setSubject(subject); | |
// 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件 | |
Multipart multipart = new MimeMultipart(); | |
//设置邮件的文本内容 | |
BodyPart contentPart = new MimeBodyPart(); | |
contentPart.setText( sendHtml.toString() ); | |
multipart.addBodyPart(contentPart); | |
//添加附件 | |
BodyPart messageBodyPart= new MimeBodyPart(); | |
//添加附件的内容 | |
InputStream is = new ByteArrayInputStream(affix); | |
DataSource dataSource1=new ByteArrayDataSource(is, "application/msexcel"); | |
DataHandler dataHandler=new DataHandler(dataSource1); | |
messageBodyPart.setDataHandler(dataHandler); | |
messageBodyPart.setFileName(MimeUtility.encodeText(affixName)); | |
//添加附件的标题 | |
multipart.addBodyPart(messageBodyPart); | |
//将multipart对象放到message中 | |
message.setContent(multipart); | |
//保存邮件 | |
message.saveChanges(); | |
getTransport().sendMessage(message, message.getAllRecipients()); | |
}catch(Exception e){ | |
e.printStackTrace(); | |
} | |
return 0; | |
} | |
public static void main(String[] args) { | |
doSendHtmlEmail( "testsubj", "<html> <a href=\"www.baidu.com\" > testyanyh </a> <h1>我的第一个标题</h1> </html>", "yanyihui@nuoyuan.com.cn"); | |
doSendAffixEmail( "testsubj", "yanyihui", "yanyihui@nuoyuan.com.cn", "e:/db.log"); | |
System.out.println("send complete"); | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment