java fmail发送邮件_Java 发送邮件

packagecom.mmall.util;importcom.sun.mail.util.MailSSLSocketFactory;importjavax.activation.DataHandler;importjavax.activation.FileDataSource;import javax.mail.*;import javax.mail.internet.*;importjava.io.UnsupportedEncodingException;importjava.security.GeneralSecurityException;import java.util.*;public classSendMailFile {//MIME邮件对象

private staticMimeMessage mimeMsg;//邮件会话对象

private staticSession session;//系统属性

private static Properties properties=System.getProperties();;//Multilpart对象,邮件内容,标题,附件等内容均添加到其中后再生成

private staticMultipart mp;//发件人用户名

private static String username="251231466@qq.com";//发件人密码

private static String password="whgodxpzprb";//发件人昵称

private static String nickname="wanghao";//收件人

private static String sendName="ssrs_wanghao@163.com";//smtp

private static String smtp="smtp.qq.com";public static voidmain(String[] args) {//设置邮件发送的SMTP主机

properties.put("mail.smtp.host", smtp);

properties.put("mail.smtp.auth", "true");if(smtp!=null && smtp.indexOf("qq")>0){//下面四行不加也可以发送 QQ邮箱设置

System.out.println("进入setQQ 加密");

MailSSLSocketFactory sf= null;try{

sf= newMailSSLSocketFactory();

}catch(GeneralSecurityException e) {

e.printStackTrace();

}assert sf != null;

sf.setTrustAllHosts(true);

properties.put("mail.smtp.ssl.enable", "true");

properties.put("mail.smtp.ssl.socketFactory", sf);

}//获取邮件会话对象

session = Session.getDefaultInstance(properties,newAuthenticator(){publicPasswordAuthentication getPasswordAuthentication()

{return new PasswordAuthentication(username, password); //发件人邮件用户名、密码

}

});//创建邮件MIME对象

mimeMsg = newMimeMessage(session);

mp= newMimeMultipart();try{

nickname= MimeUtility.encodeText(nickname, "utf-8", "B");

mimeMsg.setFrom(new InternetAddress(nickname + ""));//将收件人数组设置为InternetAddress数组类型

String [] receivers= newString[] {sendName};

List receiverList = new ArrayList();for(String receiver : receivers) {

receiverList.add(newInternetAddress(receiver));

}

InternetAddress[] addresses= receiverList.toArray(newInternetAddress[receivers.length]);

mimeMsg.setRecipients(Message.RecipientType.TO, addresses);//将抄送人数组设置为InternetAddress数组类型

String [] copyTos = new String[0];

List copyToList = new ArrayList();for(String copyto : copyTos) {

copyToList.add(newInternetAddress(copyto));

}

InternetAddress[] copyAddresses= copyToList.toArray(newInternetAddress[copyTos.length]);

mimeMsg.setRecipients(Message.RecipientType.CC, copyAddresses);

Map map = new HashMap<>();

map.put("file1","C:/Users/Administrator/Desktop/teset.txt");

map.put("file2","C:/Users/Administrator/Desktop/teset - 副本.txt");

setFileAffix(map);

mimeMsg.setSubject(MimeUtility.encodeText("主题:测试发送邮件带文件", "utf-8", "B"));//内容

BodyPart bp = newMimeBodyPart();

bp.setContent("" + "\n" +

"\n" +

"

\n" +

" \n" +

"

Title\n" +

"

" function f() {\n" +

" alert('你点击了');\n" +

" }\n" +

" \n" +

"\n" +

"

\n" +

"

这是一封测试发送邮件带附件的邮件

\n" +

"

不用回复,我就看下css样式

\n" +

"\n" +

"\n" +

"\n", "text/html;charset=utf-8");

mp.addBodyPart(bp);

mimeMsg.setContent(mp);

mimeMsg.saveChanges();

Transport transport= session.getTransport("smtp");

transport.connect((String) properties.get("mail.smtp.host"), username, password);

transport.sendMessage(mimeMsg, mimeMsg.getRecipients(Message.RecipientType.TO));

transport.close();

}catch(UnsupportedEncodingException e) {

e.printStackTrace();

}catch(AddressException e) {

e.printStackTrace();

}catch(MessagingException e) {

e.printStackTrace();

}

}/*** 添加邮件附件,附件可为多个

*@paramfileMap 文件绝对路径map集合

*@throwsMessagingException*/

public static void setFileAffix(Map fileMap) throwsMessagingException, UnsupportedEncodingException {//获取附件

for(String file: fileMap.keySet()) {//创建一个存放附件的BodyPart对象

BodyPart bp = newMimeBodyPart();//获取文件路径

String filePath =fileMap.get(file);

String[] fileNames= filePath.split("/");//获取文件名

String fileName = fileNames[fileNames.length-1];//设置附件名称,附件名称为中文时先用utf-8编码

bp.setFileName(MimeUtility.encodeWord(fileName, "utf-8", null));

FileDataSource fields= newFileDataSource(filePath);

bp.setDataHandler(newDataHandler(fields));//multipart中添加bodypart

mp.addBodyPart(bp);

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值