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寮婧愬ぇ鍏ㄤ笂鐪嬩竴涓.