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

歡迎訪問 生活随笔!

生活随笔

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

编程问答

java jmail_java jmail

發布時間:2025/3/21 编程问答 47 豆豆
生活随笔 收集整理的這篇文章主要介紹了 java jmail_java jmail 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

發送郵件的主要步驟

1.設置發送的協議,也就是設置smtp和驗證機制(一般協議都是通過Properties鍵值形式來設置)

2.發送郵件需要的幾個重要類Session ,Message,Transport

3.Session對象可以通過Session的getInstance(java.util.Properties props)

或getInstance(java.util.Properties props, Authenticator authenticator) ?Authenticator 可以理解為密碼和用戶名的驗證器

或getDefaultInstance(java.util.Properties props)

或getDefaultInstance(java.util.Properties props, Authenticator authenticator)

4.郵件的一些重要內容都是通過Message設置,比例內容,主題,接收人..

接收人可以通過Message的setRecipients(Message.RecipientType type,Address address)

Message.RecipientType 有三個重要屬性 1.BCC(密送),2.CC(抄送) ,3.TO(收件人)

5.可以通過Transport.send(Message msg)發送郵件

一般發送簡單的郵件上面幾個步驟就可以實現了,但是要發送一封復雜的郵件(有附件和文本,圖片)還需要以下步驟

A.發送復雜郵件需要這個兩個重要類MimeBodyPart 和 MimeMultipart

先要搞清楚文本和圖片的關系是related,而文本和圖片再加上附件的關系就是mixed

B.文本用一個MimeBodyPart對象保存

C.圖片也是用一個MimeBodyPart對象保存

D.用一個MimeMultipart A對象來保存,保存文本的MimeBodyPart對象和保存圖片的MimeBodyPart對象

這個MimeMultipart對象 通過構造函數 new MimeMultipart("related")獲取

E.附件也是用一個MimeBodyPart C對象保存

F.用一個總MimeBodyPart B對象來保存MimeMultipart A對象

G.還得用一個總的MimeMultipart all對象來保存MimeBodyPart C對象和MimeBodyPart B對象

這個all對象通過構造函數 new MimeMultipart("mixed")獲取

H.最后用Message.setContent(Multipart mp)方法保存總的郵件

I.可以通過Transport.send(Message msg)發送郵件

現在發送一份簡單的郵件,郵件內容只含有一些文字

Code:

package com.michael.email;

import java.util.Properties;

import javax.mail.Address;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

// @author Michael.Wu

//JavaMail 發送郵件

public class JavaEmail {

public static void main(String[] args) throws AddressException, MessagingException {

Properties properties = new Properties();

properties.setProperty("mail.transport.protocol", "smtp");//發送郵件協議

properties.setProperty("mail.smtp.auth", "true");//需要驗證

// properties.setProperty("mail.debug", "true");//設置debug模式 后臺輸出郵件發送的過程

Session session = Session.getInstance(properties);

session.setDebug(true);//debug模式

//郵件信息

Message messgae = new MimeMessage(session);

messgae.setFrom(new InternetAddress("whyao@sina.cn"));//設置發送人

messgae.setText("what's up man");//設置郵件內容

messgae.setSubject("哥們該吃飯了");//設置郵件主題

//發送郵件

Transport tran = session.getTransport();

// tran.connect("smtp.sohu.com", 25, "wuhuiyao@sohu.com", "xxxx");//連接到新浪郵箱服務器

tran.connect("smtp.sina.com", 25, "whyao@sina.cn", "xxxxxxx");//連接到新浪郵箱服務器

// tran.connect("smtp.qq.com", 25, "Michael8@qq.vip.com", "xxxx");//連接到QQ郵箱服務器

tran.sendMessage(messgae, new Address[]{ new InternetAddress("Michael8@qq.vip.com")});//設置郵件接收人

tran.close();

}

}

====================================================================================

把郵件的用戶名和密碼改為你們自己的,就可以發送郵件了。有一個地方得注意,設置發送人和和你當前 ? ? ? 發送郵件用的賬號要相同,不然會報異常或會收不到郵件。以前發送人和發送郵件的賬號是可以不同的現 ? ? ? 在就不行,可能是郵箱服務商不允許的原因吧。

接下來發送一份復雜的郵件(含有附件和圖片,文本)

Code:

package com.michael.email;

import java.io.File;

import java.util.Properties;

import javax.activation.DataHandler;

import javax.activation.FileDataSource;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.Message.RecipientType;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

//@author Michael.wu

// 發送復雜的郵件(文本內容,附件,圖片)

public class JavaEmail3 {

public static void main(String[] args) throws MessagingException {

//發送郵件的協議

Properties properties = new Properties();

properties.setProperty("mail.smtp.auth","true");//設置驗證機制

properties.setProperty("mail.transport.protocol","smtp");//發送郵件協議

properties.setProperty("mail.smtp.host","smtp.sina.com");//設置郵箱服務器地址

properties.setProperty("mail.smtp.port","25");

Session session = Session.getInstance(properties,new MyAuthenticator());

session.setDebug(true);

Message message = new MimeMessage(session);

message.setFrom(new InternetAddress("whyao@sina.cn"));

message.setSubject("一封復雜的郵件");

message.setRecipients(RecipientType.TO,InternetAddress.parse("michael8@vip.qq.com"));//接收人

message.setRecipients(RecipientType.CC,InternetAddress.parse("1348800595@qq.com"));//抄送人

message.setRecipients(RecipientType.BCC,InternetAddress.parse("1348800595@qq.com"));//密送人

MimeBodyPart bodyPartAttch = createAttachMent("C:\\Users\\Administrator\\Desktop\\mail.jar");//附件

MimeBodyPart bodyPartContentAndPic = createContentAndPic("I just want to Fuck","C:\\Users\\Administrator\\Desktop\\0.jpg");//文本內容

MimeMultipart mimeMuti = new MimeMultipart("mixed");

mimeMuti.addBodyPart(bodyPartAttch);

mimeMuti.addBodyPart(bodyPartContentAndPic);

message.setContent(mimeMuti);

message.saveChanges();

//message.setContent("Michael", "text/html;charset=gbk");

Transport.send(message);

}

//創建附件

public static MimeBodyPart createAttachMent(String path) throws MessagingException{

MimeBodyPart mimeBodyPart = new MimeBodyPart();

FileDataSource dataSource = new FileDataSource( new File(path));

mimeBodyPart.setDataHandler(new DataHandler(dataSource));

mimeBodyPart.setFileName(dataSource.getName());

return mimeBodyPart;

}

//創建文本和圖片

public static MimeBodyPart createContentAndPic(String content,String path) throws MessagingException{

MimeMultipart mimeMutiPart = new MimeMultipart("related");

//圖片

MimeBodyPart picBodyPart = new MimeBodyPart();

FileDataSource fileDataSource = new FileDataSource( new File(path));

picBodyPart.setDataHandler(new DataHandler(fileDataSource));

picBodyPart.setFileName(fileDataSource.getName());

mimeMutiPart.addBodyPart(picBodyPart);

//文本

MimeBodyPart contentBodyPart = new MimeBodyPart();

contentBodyPart.setContent(content,"text/html;charset=gbk");

mimeMutiPart.addBodyPart(contentBodyPart);

//圖片和文本結合

MimeBodyPart allBodyPart = new MimeBodyPart();

allBodyPart.setContent(mimeMutiPart);

return allBodyPart;

}

}

Code:

package com.michael.email;

import javax.mail.Authenticator;

import javax.mail.PasswordAuthentication;

public class MyAuthenticator extends Authenticator {

private static final String userName = "whyao@sina.cn";

private static final String passWord = "xxxxxxx";

// * @author Michael.wu

//* 密碼和用戶的驗證

public MyAuthenticator() {

super();

}

@Override

protected PasswordAuthentication getPasswordAuthentication() {

return new PasswordAuthentication(userName, passWord);

}

}

//注意上面通過Main方法發送出去的郵件,圖片會以附件的形式顯示。如果你想把圖片當做背景圖或者是想在文本中插一張圖片,還要把上面的createContentAndPic方法改改。

code:

//創建文本和圖片

public static MimeBodyPart createContentAndPic(String content,String path) throws MessagingException, UnsupportedEncodingException{

MimeMultipart mimeMutiPart = new MimeMultipart("related");

//圖片

MimeBodyPart picBodyPart = new MimeBodyPart();

FileDataSource fileDataSource = new FileDataSource( new File(path));

picBodyPart.setDataHandler(new DataHandler(fileDataSource));

picBodyPart.setFileName(MimeUtility.encodeText("米克個人照"));//解決中文亂碼問題

picBodyPart.setHeader("Content-Location", "http://www.michael.com/mike.jpg");

//http://www.michael.com/mike.jpg這個路徑是后面文本圖片的路徑

//文本

MimeBodyPart contentBodyPart = new MimeBodyPart();

//img的src要和setHeader中設置的值一樣

contentBodyPart.setContent(content+"","text/html;charset=gbk");

mimeMutiPart.addBodyPart(contentBodyPart);

mimeMutiPart.addBodyPart(picBodyPart);

//圖片和文本結合

MimeBodyPart allBodyPart = new MimeBodyPart();

allBodyPart.setContent(mimeMutiPart);

return allBodyPart;

}

//通過上面的程序發送出的郵件,在Foxmail中打開,圖片會在文本中正常顯示。而在QQ郵箱 新浪郵箱 打開這封郵件,圖片會以附件的形式顯示,在文本中顯示不出來。這到底是什么原因,或是還要設置什么。我還沒有弄明白。希望看到這篇博客的網友,知道是什么原因的,還得請你們也告訴我一聲,謝謝。

總結

以上是生活随笔為你收集整理的java jmail_java jmail的全部內容,希望文章能夠幫你解決所遇到的問題。

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