日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

java 邮件 附件_java中javamail发送带附件的邮件实现方法

發布時間:2023/12/31 编程问答 28 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java 邮件 附件_java中javamail发送带附件的邮件实现方法 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

本文實例講述了java中javamail發送帶附件的郵件實現方法。分享給大家供大家參考。具體分析如下:

JavaMail,顧名思義,提供給開發者處理電子郵件相關的編程接口。它是Sun發布的用來處理email的API。它可以方便地執行一些常用的郵件傳輸,JavaMail是可選包,因此如果需要使用的話你需要首先從java官網上下載。目前最新版本是JavaMail1.5.0,下面我們來看看javamail發送帶附件的郵件實例

mail.java 代碼:

package mail;

import java.util.* ;

import java.io.* ;

import javax.mail.* ;

import javax.mail.internet.* ;

import javax.activation.* ;

public class Mail {

//定義發件人、收件人、SMTP服務器、用戶名、密碼、主題、內容等

private String displayName;

private String to;

private String from;

private String smtpServer;

private String username;

private String password;

private String subject;

private String content;

private boolean ifAuth; //服務器是否要身份認證

private String filename="";

private Vector file = new Vector(); //用于保存發送附件的文件名的集合

/**

* 設置SMTP服務器地址

*/

public void setSmtpServer(String smtpServer){

this.smtpServer=smtpServer;

}

/**

* 設置發件人的地址

*/

public void setFrom(String from){

this.from=from;

}

/**

* 設置顯示的名稱

*/

public void setDisplayName(String displayName){

this.displayName=displayName;

}

/**

* 設置服務器是否需要身份認證

*/

public void setIfAuth(boolean ifAuth){

this.ifAuth=ifAuth;

}

/**

* 設置E-mail用戶名

*/

public void setUserName(String username){

this.username=username;

}

/**

* 設置E-mail密碼

*/

public void setPassword(String password){

this.password=password;

}

/**

* 設置接收者

*/

public void setTo(String to){

this.to=to;

}

/**

* 設置主題

*/

public void setSubject(String subject){

this.subject=subject;

}

/**

* 設置主體內容

*/

public void setContent(String content){

this.content=content;

}

/**

* 該方法用于收集附件名

*/

public void addAttachfile(String fname){

file.addElement(fname);

}

public Mail(){

}

/**

* 初始化SMTP服務器地址、發送者E-mail地址、用戶名、密碼、接收者、主題、內容

*/

public Mail(String smtpServer,String from,String displayName,String username,String password,String to,String subject,String content){

this.smtpServer=smtpServer;

this.from=from;

this.displayName=displayName;

this.ifAuth=true;

this.username=username;

this.password=password;

this.to=to;

this.subject=subject;

this.content=content;

}

/**

* 初始化SMTP服務器地址、發送者E-mail地址、接收者、主題、內容

*/

public Mail(String smtpServer,String from,String displayName,String to,String subject,String content){

this.smtpServer=smtpServer;

this.from=from;

this.displayName=displayName;

this.ifAuth=false;

this.to=to;

this.subject=subject;

this.content=content;

}

/**

* 發送郵件

*/

public HashMap send(){

HashMap map=new HashMap();

map.put("state", "success");

String message="郵件發送成功!";

Session session=null;

Properties props = System.getProperties();

props.put("mail.smtp.host", smtpServer);

if(ifAuth){ //服務器需要身份認證

props.put("mail.smtp.auth","true");

SmtpAuth smtpAuth=new SmtpAuth(username,password);

session=Session.getDefaultInstance(props, smtpAuth);

}else{

props.put("mail.smtp.auth","false");

session=Session.getDefaultInstance(props, null);

}

session.setDebug(true);

Transport trans = null;

try {

Message msg = new MimeMessage(session);

try{

Address from_address = new InternetAddress(from, displayName);

msg.setFrom(from_address);

}catch(java.io.UnsupportedEncodingException e){

e.printStackTrace();

}

InternetAddress[] address={new InternetAddress(to)};

msg.setRecipients(Message.RecipientType.TO,address);

msg.setSubject(subject);

Multipart mp = new MimeMultipart();

MimeBodyPart mbp = new MimeBodyPart();

mbp.setContent(content.toString(), "text/html;charset=gb2312");

mp.addBodyPart(mbp);

if(!file.isEmpty()){//有附件

Enumeration efile=file.elements();

while(efile.hasMoreElements()){

mbp=new MimeBodyPart();

filename=efile.nextElement().toString(); //選擇出每一個附件名

FileDataSource fds=new FileDataSource(filename); //得到數據源

mbp.setDataHandler(new DataHandler(fds)); //得到附件本身并至入BodyPart

mbp.setFileName(fds.getName());? //得到文件名同樣至入BodyPart

mp.addBodyPart(mbp);

}

file.removeAllElements();

}

msg.setContent(mp); //Multipart加入到信件

msg.setSentDate(new Date());???? //設置信件頭的發送日期

//發送信件

msg.saveChanges();

trans = session.getTransport("smtp");

trans.connect(smtpServer, username, password);

trans.sendMessage(msg, msg.getAllRecipients());

trans.close();

}catch(AuthenticationFailedException e){

map.put("state", "failed");

message="郵件發送失敗!錯誤原因:\n"+"身份驗證錯誤!";

e.printStackTrace();

}catch (MessagingException e) {

message="郵件發送失敗!錯誤原因:\n"+e.getMessage();

map.put("state", "failed");

e.printStackTrace();

Exception ex = null;

if ((ex = e.getNextException()) != null) {

System.out.println(ex.toString());

ex.printStackTrace();

}

}

//System.out.println("\n提示信息:"+message);

map.put("message", message);

return map;

}

}

SmtpAuth.java 代碼:

package mail;

public class SmtpAuth extends javax.mail.Authenticator {

private String username,password;

public SmtpAuth(String username,String password){

this.username = username;

this.password = password;

}

protected javax.mail.PasswordAuthentication getPasswordAuthentication() {

return new javax.mail.PasswordAuthentication(username,password);

}

}

存在的問題就是發送到163的郵件全部都帶有一個附件的符號,不管有沒有發送附件,感興趣的朋友可以對此加以改進和完善。

希望本文所述對大家的Java程序設計有所幫助。

總結

以上是生活随笔為你收集整理的java 邮件 附件_java中javamail发送带附件的邮件实现方法的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。