java搭建郵件伺服器
① 奼備竴鐢↗AVA鍋氱殑涓涓鍙戦偖浠剁殑紼嬪簭(鎬)!
榪欐槸鎴戜互鍓嶇敤鐨 鐜板湪鏈変簺闇瑕侀獙璇佺殑閭綆卞彲鐢ㄧ敤涓嶄簡錛岀敤涓嶄簡鐨勮瘽HI鎴
package mail;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class SendMail{
public static void main(String[] args) {
send("鏍囬","鍐呭");
}
public static void send(String h ,String b ) {
try {
Properties p = new Properties(); //Properties p = System.getProperties();
p.put("mail.smtp.auth", "true");
p.put("mail.transport.protocol", "smtp");
p.put("mail.smtp.host", "smtp.139.com");
p.put("mail.smtp.port", "25");
//寤虹珛浼氳瘽
Session session = Session.getInstance(p);
MimeMessage msg = new MimeMessage(session); //寤虹珛淇℃伅
msg.setFrom(new InternetAddress("[email protected]")); //鍙戜歡浜
// msg.setRecipient(MimeMessage.RecipientType.TO,
// new InternetAddress("[email protected] [email protected]")); //鏀朵歡浜
Address []address=new Address[]{new InternetAddress("[email protected]"),new InternetAddress("[email protected]")};
msg.setRecipients(MimeMessage.RecipientType.TO,
address); //鏀朵歡浜
msg.setSentDate(new Date()); // 鍙戦佹棩鏈
msg.setSubject(h); // 涓婚
msg.setText(b); //鍐呭
// 閭浠舵湇鍔″櫒榪涜岄獙璇
Transport tran = session.getTransport("smtp");
tran.connect("smtp.139.com", "bluebit_cn", "xiaohao");
// bluebit_cn鏄鐢ㄦ埛鍚嶏紝xiaohao鏄瀵嗙爜
tran.sendMessage(msg, msg.getAllRecipients()); // 鍙戦
//System.out.println("閭浠跺彂閫佹垚鍔");
} catch (Exception e) {
e.printStackTrace();
}
}}
② java mail 連接郵件伺服器
不知有幫助沒有了
<%@ page
import=" javax.mail.*, javax.mail.internet.*, javax.activation.*,java.util.*"
%>
<html>
<head>
<TITLE>JSP meets JavaMail, what a sweet combo.</TITLE>
</HEAD>
<BODY>
<%
try{
Properties props = new Properties();
Session sendMailSession;
Store store;
Transport transport;
sendMailSession = Session.getInstance(props, null);
props.put("mail.smtp.host", "smtp.jspinsider.com");
Message newMessage = new MimeMessage(sendMailSession);
newMessage.setFrom(new InternetAddress(request.getParameter("from")));
newMessage.setRecipient(Message.RecipientType.TO, new InternetAddress(request.getParameter("to")));
newMessage.setSubject(request.getParameter("subject"));
newMessage.setSentDate(new Date());
newMessage.setText(request.getParameter("text"));
transport = sendMailSession.getTransport("smtp");
transport.send(newMessage);
%>
<P>Your mail has been sent.</P>
<%
}
catch(MessagingException m)
{
out.println(m.toString());
}
%>
</BODY>
</HTML>
③ Java收發郵件過程中具體的功能是怎麼實現的
1.SMTP協議
用戶連上郵件伺服器後,要想給它發送一封電子郵件,需要遵循一定的通迅規則,SMTP協議就是用於定義這種通訊規則的。
因而,通常我們也把處理用戶smtp請求(郵件發送請求)的郵件伺服器稱之為SMTP伺服器。(25)
2.POP3協議
同樣,用戶若想從郵件伺服器管理的電子郵箱中接收一封電子郵件的話,他連上郵件伺服器後,也需要遵循一定的通迅格式,POP3協議用於定義這種通訊格式。
因而,通常我們也把處理用戶pop3請求(郵件接收請求)的郵件伺服器稱之為POP3伺服器。(110)
下圖用於演示兩帳戶相互發送郵件的過程
packagecn.e.dlmu.send;
importjava.util.Properties;
importjavax.activation.DataHandler;
importjavax.activation.FileDataSource;
importjavax.mail.Message;
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;
publicclassSendMail{
publicstaticvoidmain(String[]args)throwsException{
Propertiesprop=newProperties();
//連接的郵件伺服器的主機名
prop.setProperty("mail.smtp.host","smtp.sina.com.cn");
//發送郵件的協議
prop.setProperty("mail.transport.protocol","smtp");
//是否向郵件伺服器提交認證
prop.setProperty("mail.smtp.auth","true");
//創建session
Sessionsession=Session.getInstance(prop);
session.setDebug(true);
//得到transport
Transportts=session.getTransport();
//連接郵件伺服器
ts.connect("smtp.sina.com.cn","[email protected]","xxxxx");
//發送郵件
MimeMessagemessage=createMessage(session);
ts.sendMessage(message,message.getAllRecipients());
ts.close();
}
(Sessionsession)throwsException{
MimeMessagemessage=newMimeMessage(session);
//設置郵件的基本信息
message.setFrom(newInternetAddress("[email protected]"));
message.setRecipient(Message.RecipientType.TO,newInternetAddress("[email protected]"));
message.setSubject("test");
//正文
MimeBodyParttext=newMimeBodyPart();
//設置charaset可以解決中文正文的亂碼問題,內嵌可下載的圖片
text.setContent("你好xxx,<imgsrc='c:/dog.jpg'/>測試成功!<br/><imgsrc='cid:aaa.jpg'/>","text/html;charset=gbk");
//圖片1
MimeBodyPartimage=newMimeBodyPart();
image.setDataHandler(newDataHandler(newFileDataSource("src/88.jpg")));
image.setContentID("aaa.jpg");
//附件
MimeBodyPartattach=newMimeBodyPart();
DataHandlerdh=newDataHandler(newFileDataSource("src/javamail架包.jar"));
attach.setDataHandler(dh);
//解決文件中文亂碼問題
attach.setFileName(MimeUtility.encodeText(dh.getName()));
//描述正文和圖片的關系
MimeMultipartmp=newMimeMultipart();
mp.addBodyPart(text);
mp.addBodyPart(image);
mp.setSubType("related");
//描述正文和附件
MimeMultipartmp2=newMimeMultipart();
mp2.addBodyPart(attach);
//將正文封裝為一個body
MimeBodyPartcontent=newMimeBodyPart();
content.setContent(mp);
mp2.addBodyPart(content);
mp2.setSubType("mixed");
message.setContent(mp2);
message.saveChanges();
returnmessage;
}
}
④ 如何在本機上利用java實現smtp郵件伺服器
1.修改<postmaster>localhost</postmaster>,改成你自己的伺服器域名,比如:<postmaster>support@</postmaster>
2.修改這些,也是改成自己的
<servernames autodetect="false" autodetectIP="false">
<servername></servername>
</servernames>
3.修改dns server,這是為了可以發外網郵件,查看DNS地址使用ipconfig/all,然後在配置文件中dnsserver節點下加入server地址.
⑤ java 閭浠舵湇鍔″櫒鐢ㄤ粈涔堟瘮杈冨ソ鍟
璺ㄥ鉤鍙扮殑璇濅篃鍙浠ヨ瘯璇旳pache James錛岀函Java鐨勯偖浠舵湇鍔″櫒錛屽緢澶氫紒涓氬湪鐢ㄣ傝嚦浜巔ostfix鍗昉C姣忔棩搴斾粯鐧句竾綰х殑閭浠跺簲璇ヤ笉鎴愰棶棰橈紝瑕侀厤緗鐨勮瘽鐩稿叧璁劇疆鍙鑳借佽姳浜涘姛澶錛屽ソ鍦ㄦ枃妗e緢鍏ㄩ潰錛屼嬌鐢ㄤ篃寰堝箍娉涖備笉榪囨棦鐒惰侀儴緗插埌linux浣曚笉灝卞湪linux涓嬪紑鍙戝憿錛熺幇鍦ㄥ緢澶歭inux desktop鍋氬緱鎸烘槗鐢ㄧ殑錛屼粎鏄疛ava web寮鍙戠殑璇濅笂鎵嬩笉闅撅紝涓嶅Θ鐩存帴鍦║buntu鎴朣USE涓嬪紑鍙戱紝鐒跺悗鍐嶅線鐩稿簲鐨凷erver鐗堜笂閮ㄧ講錛岃繖鏍風▼搴忎慨鏀圭殑姣旇緝灝忋
⑥ java中如何實現公司郵箱發送郵件配置
Java中可以通過Javamail API實現公司郵箱郵件發送配置,Java mail是利用現有的郵箱賬戶發送郵件的工具,具體步驟如如下:
1、通過JavamailAPI設置發送者郵箱用戶名及密碼
2、通過JavamailAPI設置郵件主題、郵件內容、附件及郵件發送時間
3、通過JavamailAPI設置發送者郵箱地址及接收者郵箱地址,接收者地址可以是多個及抄送
4、郵件的需基本元素都設置完畢後,即可通過Javamail API的發送介面執行發送操作。
⑦ 璇烽珮浜烘寚鐐逛竴涓嬶紝java鍙浠ュ仛閭綆變箞
鎴戜篃娌℃悶娓呮氫綘瑕佸仛閭綆辯殑浠涔?鏄閭浠舵湇鍔″櫒?榪樻槸瀹㈡埛絝?涓涓瀹屾暣鐨勯偖綆卞彲涓嶆槸閭d箞綆鍗曠殑,浣犺繛鍋氫粈涔堥兘娌℃湁鎼炴竻妤氭庝箞鍋.java褰撶劧鍙浠ュ仛浜.姣忕嶈璦閮藉彲浠ュ仛鐨,apache緗戠珯灝辨湁java寮婧愮殑閭浠舵湇鍔$郴緇(james),瀹㈡埛絝鍦ㄧ綉涓婁篃鏈夊緢澶.鍙浠ュ埌java寮婧愬ぇ鍏ㄤ笂鐪嬩竴涓.