📄 mailsend.java
字号:
package EmailManage;
/**
* Vector v1=new vector(); for (int i = 0; i < cboHtmlPic.length(); i++) {
* FoldFile = cboHtmlPic[i]; fname =
* FoldFile.substring(FoldFile.lastIndexOf("\\") + 1);// 提取文件名 if
* (cHtml.lastIndexOf(FoldFile) > 0) { v1.add(arg0); cboAtt.addItem(FoldFile); }
* cHtml = cHtml.replaceAll(fname, "cid:" + fname); } Security.addProvider(new
* com.sun.net.ssl.internal.ssl.Provider()); final String SSL_FACTORY =
* "javax.net.ssl.SSLSocketFactory"; Properties props = System.getProperties();
* props.setProperty("mail.smtp.host", "smtp.gmail.com");
* props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
* props.setProperty("mail.smtp.socketFactory.fallback", "false");
* props.setProperty("mail.smtp.port", "465");
* props.setProperty("mail.smtp.socketFactory.port", smtpPort);
* props.put("mail.smtp.auth", "true"); Session session =
* Session.getDefaultInstance(props, new Authenticator() { protected
* PasswordAuthentication getPasswordAuthentication() { return new
* PasswordAuthentication(username, password);//身份验证 }});
*/
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.security.Security;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Properties;
import java.util.Vector;
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Authenticator;
import javax.mail.BodyPart;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.internet.AddressException;
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;
/**
* String smtpHost = "smtp.gmail.com"; String to = "dongrujun@126.com"; String
* from = "dongrujun@gmail.com"; final String username = "dongrujun"; final
* String password = "000229"; String subject = "测试 邮件的html源代码";
*/
public class MailSend {
public MimeMessage message;
public String charset = "charset=GBK";
public String TextHtmlType = "text/html;";
public String smtpHost;
public String smtpPort = "25";
public boolean ssl = false;
public boolean authentification = false;
public String UserName = "";
public String PassWord = "";
public String dateformat = "yyyy-MM-dd HH:mm";
public String From = "";
private String[] to;
private String[] cc=null;
private String[] bcc=null;
private String Priority;
private String subject;
private String BodyText; // HTML文本
private String BodyTextNotModify; // HTML文本
private Vector vAttachment; // 附件列表及 HTML文本中图片列表
private boolean Notification_To;//Disposition-Notification-To
public void EmailSend(
) throws MessagingException {
final String username = UserName;
final String password = PassWord;
Session _session;
final Properties props = new Properties();
props.setProperty("mail.smtp.host", smtpHost);
props.setProperty("mail.smtp.port", smtpPort);
if (ssl) { // Connection SSL
final String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
props.setProperty("mail.smtp.socketFactory.class", SSL_FACTORY);
props.setProperty("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.auth", "true");
props.setProperty("mail.smtp.socketFactory.port", smtpPort);
}
if (authentification = false) {
_session = Session.getDefaultInstance(props, null);
} else {
props.put("mail.smtp.auth", "true");
_session = Session.getDefaultInstance(props, new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
}
message = new MimeMessage(_session);
message.setFrom(new InternetAddress(this.From));
if (this.Priority != null)
message.addHeader("X-Priority", this.Priority);// 优先级
if (Notification_To == true)
message.addHeader("Disposition-Notification-To", this.From);
if (this.to !=null)
message.setRecipients(Message.RecipientType.TO, this.getto());
if (this.cc !=null)
message.setRecipients(Message.RecipientType.CC, this.getcc());
if (this.bcc !=null)
message.setRecipients(Message.RecipientType.BCC, this.getbcc());
message.setSubject(this.subject);
message.setSentDate(new Date());
MimeMultipart MultPart = new MimeMultipart();// 新建一个MimeMultipart对象用来存放BodyPart对象(事实上能够存放多个)
BodyPart BodyPart = new MimeBodyPart();// 新建一个存放信件内容的BodyPart对象
SetHtmlTextPic();
try {
BodyPart.setContent(MimeUtility.encodeText(BodyText), TextHtmlType + charset);
} catch (UnsupportedEncodingException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}// 给BodyPart对象配置内容和格式/编码方式
MultPart.setSubType("related");// 这句很重要,千万不要忘了
MultPart.addBodyPart(BodyPart);
AddAttachment( MultPart); //添加附件
message.setContent(MultPart); // 消息对象的内容
message.saveChanges();
/** int() new Random();
SimpleDateFormat format = new SimpleDateFormat(dateformat);
String ff = format.format(new Date()); //+ ".eml";
ff=ff.replaceAll(" ", "");
ff=ff.replaceAll("-", "");
ff=ff.replaceAll(":", "");
File file = new File("c:\\"+ff+".eml");
try {
message.writeTo(new FileOutputStream(file));
} catch (FileNotFoundException e) {
e.printStackTrace();
System.out.println("保存文件失败!");
} catch (IOException e) {
e.printStackTrace();
System.out.println("保存文件失败!");
}
*/
javax.mail.Transport transport = _session.getTransport("smtp");
transport.connect(smtpHost, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
/**
* 添加附件
* @param MultPart
* @return
* @throws MessagingException
*/
private MimeMultipart AddAttachment(MimeMultipart MultPart) throws MessagingException{
FileDataSource datasource = null;
BodyPart FilePart = null;
String fname = "";
String FoldFile = "";
String MimeFileName;
for (int i = 0; i < vAttachment.size(); i++) {
FilePart = new MimeBodyPart();// 新建一个存放附件的BodyPart
FoldFile = vAttachment.get(i).toString();
fname = FoldFile.substring(FoldFile.lastIndexOf("\\") + 1);// 提取文件名
datasource = new FileDataSource(FoldFile);
try {
FilePart.setDataHandler(new DataHandler(datasource));
MimeFileName=MimeUtility.encodeText(fname);
FilePart.setFileName(MimeFileName);
if (BodyTextNotModify.lastIndexOf(FoldFile) > 0) {
FilePart.setFileName(MimeFileName);
FilePart.setDisposition("inline");
}
else{
FilePart.setFileName(MimeFileName);
}
FilePart.setHeader("Content-ID", MimeFileName);
MultPart.addBodyPart(FilePart);
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
return MultPart;
}
private void SetHtmlTextPic(){
String fname = "";
String FoldFile = "";
for (int i = 0; i < vAttachment.size(); i++) {
FoldFile = vAttachment.get(i).toString();
fname = FoldFile.substring(FoldFile.lastIndexOf("\\") + 1);// 提取文件名
if (BodyText.lastIndexOf(FoldFile)>0 )
{
// fExtend=fname.substring(fname.lastIndexOf("."));// MimeUtility.decodeText(attachmentFile);
try {
BodyText = BodyText.replace(FoldFile, "cid:" + MimeUtility.encodeText(fname));
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}}
}
public InternetAddress[] getto() throws AddressException {
if ( to != null) {
InternetAddress[] sendTo = new InternetAddress[to.length];
for (int i = 0; i < to.length; i++) {
sendTo[i] = new InternetAddress(to[i]);
}
return sendTo;}
else { return null;}
}
public void setto(String[] to) {
this.to = to;
}
public InternetAddress[] getcc() throws AddressException {
if ( cc != null) {
InternetAddress[] sendTo = new InternetAddress[cc.length];
for (int i = 0; i < cc.length; i++) {
sendTo[i] = new InternetAddress(cc[i]);
}
return sendTo;}
else { return null;}
}
public void setcc(String[] cc) {
this.cc = cc;
}
public InternetAddress[] getbcc() throws AddressException {
if ( bcc != null) {
InternetAddress[] sendTo = new InternetAddress[bcc.length];
for (int i = 0; i < bcc.length; i++) {
sendTo[i] = new InternetAddress(bcc[i]);
}
return sendTo;}
else { return null;}
}
public void setbcc(String[] bcc) {
this.bcc = bcc;
}
public String getSubject() {
return subject;
}
public void setSubject(String subject) {
this.subject = subject;
}
public Vector getVAttachment() {
return vAttachment;
}
public void setVAttachment(Vector attachment) {
vAttachment = attachment;
}
public String getPriority() {
return Priority;
}
public void setPriority(String priority) {
Priority = priority;
}
public String getCBodyText() {
return BodyText;
}
public void setBodyText(String bodyText) {
BodyText = bodyText;
BodyTextNotModify = bodyText;
}
public void setNotification_To(boolean notification_To) {
Notification_To = notification_To;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -