发邮件代码
来源:互联网 发布:加密狗写入软件 编辑:程序博客网 时间:2024/05/17 22:11
package com.anhry.app.safety.util;
import java.io.File;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
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;
public class SendMailUtil {
/* static int port ;
static String server ;
static String from ;
static String user ;
static String password ;*/
private static final String charset = "UTF-8";
private static final String defaultMimetype = "text/plain";
private static final Environment ENV = Environment.getInstance();
public static void main(String[] args) throws Exception {
send(new String[]{"bbheguifang@sina.com"}, "邮件测试",",<b>安宏壑业</b> .如<b>中国 </b>==,<img src='http://cs/csfile/uploadfile/personnel_pic/1367830616374.jpg' width='100'/>", null , "text/html");
}
/**
* 发送邮件
* @param receiver 收件人
* @param subject 标题
* @param mailContent 邮件内容
* @param mimetype 内容类型 默认为text/plain,如果要发送HTML内容,应设置为text/html
*/
public static void send(String receiver, String subject, String mailContent, String mimetype) {
send(new String[]{receiver}, subject, mailContent, mimetype);
}
public static void send(String receiver, String subject, String mailContent) {
send(new String[]{receiver}, subject, mailContent, "text/plain");
}
/**
* 发送邮件
* @param receivers 收件人
* @param subject 标题
* @param mailContent 邮件内容
* @param mimetype 内容类型 默认为text/plain,如果要发送HTML内容,应设置为text/html
*/
public static void send(String[] receivers, String subject, String mailContent, String mimetype) {
send(receivers, subject, mailContent, null, mimetype);
}
/**
* 发送邮件
* @param receivers 收件人
* @param subject 标题
* @param mailContent 邮件内容
* @param attachements 附件
* @param mimetype 内容类型 默认为text/plain,如果要发送HTML内容,应设置为text/html
*/
public static void send(String[] receivers, String subject, String mailContent, File[] attachements, String mimetype) {
Properties props = new Properties();
props.put("mail.debug", ENV.getProperty("mail.debug"));
props.put("mail.smtp.host", ENV.getProperty("mail.smtp.host"));//smtp服务器地址 sohu
props.put("mail.smtp.auth", ENV.getProperty("mail.smtp.auth"));//需要校验
props.setProperty("mail.smtp.port", ENV.getProperty("mail.smtp.port"));
if("smtp.gmail.com".equals(ENV.getProperty("mail.smtp.host"))) {
props.setProperty("mail.smtp.socketFactory.class", ENV.getProperty("mail.smtp.socketFactory.class"));
props.setProperty("mail.smtp.socketFactory.fallback", ENV.getProperty("mail.smtp.socketFactory.fallback"));
props.setProperty("mail.smtp.port", ENV.getProperty("mail.smtp.port"));
props.setProperty("mail.smtp.socketFactory.port", ENV.getProperty("mail.smtp.socketFactory.port"));
}
Session session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(ENV.getProperty("mail.username"),ENV.getProperty("mail.userpasswod"));//登录用户名/密码
}
});
session.setDebug(false);
try {
MimeMessage mimeMessage = new MimeMessage(session);
//如果用网易的这个地方必须和登录的用户名一致 否则会有533 的错误
mimeMessage.setFrom(new InternetAddress(ENV.getProperty("email.from"),ENV.getProperty("email.from.display")));//发件人邮件
InternetAddress[] toAddress = new InternetAddress[receivers.length];
for (int i=0; i<receivers.length; i++) {
toAddress[i] = new InternetAddress(receivers[i]);
}
mimeMessage.setRecipients(Message.RecipientType.TO, toAddress);//收件人邮件
mimeMessage.setSubject(subject, charset);
Multipart multipart = new MimeMultipart();
//正文
MimeBodyPart body = new MimeBodyPart();
// body.setText(message, charset);不支持html
body.setContent(mailContent, (mimetype!=null && !"".equals(mimetype) ? mimetype : defaultMimetype)+ ";charset="+ charset);
multipart.addBodyPart(body);//发件内容
//附件
if(attachements!=null){
for (File attachement : attachements) {
MimeBodyPart attache = new MimeBodyPart();
//ByteArrayDataSource bads = new ByteArrayDataSource(byte[],"application/x-any");
attache.setDataHandler(new DataHandler(new FileDataSource(attachement)));
String fileName = getLastName(attachement.getName());
attache.setFileName(MimeUtility.encodeText(fileName, charset, null));
multipart.addBodyPart(attache);
}
}
mimeMessage.setContent(multipart);
// SimpleDateFormat formcat = new SimpleDateFormat("yyyy-MM-dd");
mimeMessage.setSentDate(new Date());//formcat.parse("2010-5-23")
Transport.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
private static String getLastName(String fileName) {
System.out.println(fileName);
int pos = fileName.lastIndexOf("\\");
if (pos > -1) {
fileName = fileName.substring(pos + 1);
}
pos = fileName.lastIndexOf("/");
if (pos > -1) {
fileName = fileName.substring(pos + 1);
}
System.out.println(fileName);
return fileName;
}
}
- 发邮件代码
- Python发邮件代码
- 发邮件代码
- 发邮件相关代码
- java 发邮件 代码
- 代码 laravel发邮件
- 代码 laravel发邮件
- 打电话、发短信、发邮件常用代码
- 打电话、发短信、发邮件常用代码
- 打电话、发短信、发邮件常用代码
- 打电话、发短信、发邮件常用代码
- 打电话、发短信、发邮件常用代码
- jmail发邮件的代码
- 最好的发邮件代码
- JavaMail接发邮件代码
- python发邮件的代码
- Android发邮件代码实例
- SQL发邮件过程代码
- RedHat5.8下ORALCE11g 安装
- OpenGL分格化和二次方程表面
- iOS多线程编程指南
- 免费使用的图表控件XML/SWF Charts 5.08
- 黑马程序员__7K面试题之交通灯管理系统
- 发邮件代码
- hdu 1067 BFS + hash
- 黑马程序员——交通灯管理系统
- [C/C++]_[十六进制字符串转换数值并写入文件]
- Permutation Sequence
- 故障恢复控制台在系统中的作用是什么?【系统收藏】
- PHPLinq :用SQL语法操作PHP数组。
- asp.net mvc4 中自动生成的权限管理表的说明
- sql server trigger 触发器