導航:首頁 > 編程語言 > javasmtp伺服器

javasmtp伺服器

發布時間:2025-01-06 09:12:13

A. 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);//
}
}

B. java mail 發郵件連接不上smtp伺服器怎麼

SMTP的是「Simple Mail Transfer Protocol」,即簡單郵件傳輸協議。是一組用於從源地址到目的地址傳輸郵件的規范,通過控制郵件的中轉方式。SMTP 協議屬於 TCP/IP 協議簇,幫助每台計算機在發送或中轉信件時找到下一個目的地。SMTP 伺服器就是遵循 SMTP 協議的發送郵件伺服器。SMTP 認證,簡單地說就是要求必須在提供了賬戶名和密碼之後才可以登錄 SMTP 伺服器,這就使得那些垃圾郵件的散播者無可乘之機。增加 SMTP 認證的目的是為了使用戶避免受到垃圾郵件的侵擾。

(1)可以嘗試換一個郵箱伺服器試一試.這個是可以連接上的

閱讀全文

與javasmtp伺服器相關的資料

熱點內容
命令式介面 瀏覽:399
電腦伺服器域名地址怎麼查 瀏覽:340
什麼行業要用編程 瀏覽:297
三洋立風櫃壓縮機 瀏覽:296
微拍app為什麼下載不了了 瀏覽:257
非常好的期貨5分鍾公式源碼 瀏覽:4
linuxcentos7安裝 瀏覽:691
華為網盤文件夾加密 瀏覽:74
安卓手機什麼真人游戲好玩 瀏覽:772
崑山加密軟體需求 瀏覽:270
蘋果照片壓縮包怎麼打開 瀏覽:796
檢測溫濕度和二氧化碳的單片機 瀏覽:964
安卓手機雨滴怎麼隱藏 瀏覽:587
pdf文件轉換器word 瀏覽:987
vscodepython模塊方法 瀏覽:344
如何知道伺服器有什麼漏洞 瀏覽:902
java電商訂單支付源碼 瀏覽:102
android手機滑鼠 瀏覽:465
php支付項目經驗 瀏覽:931
中國人民銀行在哪裡下載app 瀏覽:562