導航:首頁 > 編程語言 > java郵件內容

java郵件內容

發布時間:2025-09-20 11:43:23

1. java mail中Authenticator這個類到底有什麼作用

首先,要矯正你的一個錯誤,Authenticator這個類是在Java.net.*;中的。
Authenticator 類表示懂得如何獲得網路連接驗證的對象。通常,它通過提示用戶輸入信息來完成此操作。

2. 怎麼寫java技術郵件

寫作思路:一封郵件的基本內容通常分為三部分:收件人、主題、正文。主題要突出郵件的主旨,要讓別人一看到這個主題就能大概知道你想傳遞的是什麼事情。意思明確,引人注目。

如下:

尊敬的xx公司領導:

建立本站的主要目的是為了通過網路應接一些與java技術相關的一些項目,例如基於j2ee的jsp、struts等開發web網站;或基於j2se開發應用軟體件。

建立本站除了希望增加接項目和尋找編程相關的兼職的途徑外,還希望能通過本網站結交更多的軟體開發人士,交流軟體開發知識和java編程技術,更好地提高自已的軟體開發技術。

此致

敬禮!

3. 如何使用Java發送qq郵件

方法:

1.前提准備工作:
首先,郵件的發送方要開啟POP3 和SMTP服務--即發送qq郵件的賬號要開啟POP3 和SMTP服務

2.開啟方法:
登陸qq郵箱
3.點擊 設置

4.點擊—-賬戶

5.找到:POP3/IMAP/SMTP/Exchange/CardDAV/CalDAV服務 —點擊開啟

6.送簡訊 —–點擊確定

7.稍等一會,很得到一個授權碼! –注意:這個一定要記住,一會用到

8.點擊保存修改 —OK 完成

9.java 測試代碼:
package cn.cupcat.test;
import java.util.Properties;
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;
import javax.mail.internet.MimeMessage.RecipientType;
public class SendmailUtil {
public static void main(String[] args) throws AddressException, MessagingException {

Properties properties = new Properties();
properties.put("mail.transport.protocol", "smtp");// 連接協議
properties.put("mail.smtp.host", "smtp.qq.com");// 主機名
properties.put("mail.smtp.port", 465);// 埠號
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.ssl.enable", "true");//設置是否使用ssl安全連接 ---一般都使用
properties.put("mail.debug", "true");//設置是否顯示debug信息 true 會在控制台顯示相關信息
//得到回話對象
Session session = Session.getInstance(properties);
// 獲取郵件對象
Message message = new MimeMessage(session);
//設置發件人郵箱地址
message.setFrom(new InternetAddress("[email protected]"));
//設置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("[email protected]") });
//設置郵件標題
message.setSubject("這是第一封Java郵件");
//設置郵件內容
message.setText("內容為: 這是第一封java發送來的郵件。");
//得到郵差對象
Transport transport = session.getTransport();
//連接自己的郵箱賬戶
transport.connect("[email protected]", "vvctybgbvvophjcj");//密碼為剛才得到的授權碼
//發送郵件 transport.sendMessage(message, message.getAllRecipients());
}
}
10.運行就會發出郵件了。。。。
下面是我收到郵件的截圖,當然我把源碼中的郵件地址都是修改了,不是真實的,你們測試的時候,可以修改能你們自己的郵箱。最後,祝你也能成功,如果有什麼問題,可以一起討論!

注意事項

得到的授權碼一定要保存好,程序中要使用

4. java 代碼發郵件怎麼添加附件

實現java發送郵件的過程大體有以下幾步:

准備一個properties文件,該文件中存放SMTP伺服器地址等參數。

利用properties創建一個Session對象

利用Session創建Message對象,然後設置郵件主題和正文

利用Transport對象發送郵件

需要的jar有2個:activation.jar和mail.jar發送附件,需要用到Multipart對象。

importjava.io.File;
importjava.io.IOException;
importjava.io.InputStream;
importjava.util.Properties;

importjavax.activation.DataHandler;
importjavax.activation.DataSource;
importjavax.activation.FileDataSource;
importjavax.mail.BodyPart;
importjavax.mail.Message;
importjavax.mail.MessagingException;
importjavax.mail.Multipart;
importjavax.mail.Session;
importjavax.mail.Transport;
importjavax.mail.internet.InternetAddress;
importjavax.mail.internet.MimeBodyPart;
importjavax.mail.internet.MimeMessage;
importjavax.mail.internet.MimeMultipart;
importjavax.mail.internet.MimeUtility;

{
privateMimeMessagemessage;
privateSessionsession;
privateTransporttransport;

privateStringmailHost="";
privateStringsender_username="";
privateStringsender_password="";

privatePropertiesproperties=newProperties();

/*
*初始化方法
*/
publicJavaMailWithAttachment(booleandebug){
InputStreamin=JavaMailWithAttachment.class.getResourceAsStream("MailServer.properties");
try{
properties.load(in);
this.mailHost=properties.getProperty("mail.smtp.host");
this.sender_username=properties.getProperty("mail.sender.username");
this.sender_password=properties.getProperty("mail.sender.password");
}catch(IOExceptione){
e.printStackTrace();
}

session=Session.getInstance(properties);
session.setDebug(debug);//開啟後有調試信息
message=newMimeMessage(session);
}

/**
*發送郵件
*
*@paramsubject
*郵件主題
*@paramsendHtml
*郵件內容
*@paramreceiveUser
*收件人地址
*@paramattachment
*附件
*/
publicvoiddoSendHtmlEmail(Stringsubject,StringsendHtml,StringreceiveUser,Fileattachment){
try{
//發件人
InternetAddressfrom=newInternetAddress(sender_username);
message.setFrom(from);

//收件人
InternetAddressto=newInternetAddress(receiveUser);
message.setRecipient(Message.RecipientType.TO,to);

//郵件主題
message.setSubject(subject);

//向multipart對象中添加郵件的各個部分內容,包括文本內容和附件
Multipartmultipart=newMimeMultipart();

//添加郵件正文
BodyPartcontentPart=newMimeBodyPart();
contentPart.setContent(sendHtml,"text/html;charset=UTF-8");
multipart.addBodyPart(contentPart);

//添加附件的內容
if(attachment!=null){
BodyPartattachmentBodyPart=newMimeBodyPart();
DataSourcesource=newFileDataSource(attachment);
attachmentBodyPart.setDataHandler(newDataHandler(source));

//網上流傳的解決文件名亂碼的方法,其實用MimeUtility.encodeWord就可以很方便的搞定
//這里很重要,通過下面的Base64編碼的轉換可以保證你的中文附件標題名在發送時不會變成亂碼
//sun.misc.BASE64Encoderenc=newsun.misc.BASE64Encoder();
//messageBodyPart.setFileName("=?GBK?B?"+enc.encode(attachment.getName().getBytes())+"?=");

//MimeUtility.encodeWord可以避免文件名亂碼
attachmentBodyPart.setFileName(MimeUtility.encodeWord(attachment.getName()));
multipart.addBodyPart(attachmentBodyPart);
}

//將multipart對象放到message中
message.setContent(multipart);
//保存郵件
message.saveChanges();

transport=session.getTransport("smtp");
//smtp驗證,就是你用來發郵件的郵箱用戶名密碼
transport.connect(mailHost,sender_username,sender_password);
//發送
transport.sendMessage(message,message.getAllRecipients());

System.out.println("sendsuccess!");
}catch(Exceptione){
e.printStackTrace();
}finally{
if(transport!=null){
try{
transport.close();
}catch(MessagingExceptione){
e.printStackTrace();
}
}
}
}

publicstaticvoidmain(String[]args){
JavaMailWithAttachmentse=newJavaMailWithAttachment(true);
Fileaffix=newFile("c:\測試-test.txt");
se.doSendHtmlEmail("郵件主題","郵件內容","[email protected]",affix);//
}
}
閱讀全文

與java郵件內容相關的資料

熱點內容
銑外圓格式編程視頻 瀏覽:237
掛壁式空調壓縮機 瀏覽:699
通信伺服器是什麼意思 瀏覽:385
私有雲與伺服器有什麼區別 瀏覽:371
設計源碼是什麼 瀏覽:507
nioslinux 瀏覽:452
程序員用多大筆記本 瀏覽:672
游戲腳本高級編程英文版 瀏覽:507
從不懂編程到成一個大神 瀏覽:407
pdf轉換器永久破解版 瀏覽:338
java郵件內容 瀏覽:128
如何正確地解壓 瀏覽:188
f盤里的圖片文件夾可以刪嗎 瀏覽:12
寵物大進化源碼 瀏覽:748
英雄聯盟手游微信區安卓怎麼掃碼 瀏覽:715
如何買雲伺服器價格對比 瀏覽:652
簡述linux源碼安裝步驟 瀏覽:96
單片機張毅剛答案 瀏覽:676
究極風暴3壓縮包 瀏覽:533
28歲程序員真實身份 瀏覽:176