package me.gacl.main;
金鳳網(wǎng)站建設(shè)公司創(chuàng)新互聯(lián)建站,金鳳網(wǎng)站設(shè)計(jì)制作,有大型網(wǎng)站制作公司豐富經(jīng)驗(yàn)。已為金鳳千余家提供企業(yè)網(wǎng)站建設(shè)服務(wù)。企業(yè)網(wǎng)站搭建\成都外貿(mào)網(wǎng)站建設(shè)公司要多少錢,請找那個(gè)售后服務(wù)好的金鳳做網(wǎng)站的公司定做!
import java.util.Properties;
import javax.mail.Message;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Sendmail {
/**
* @param args
* @throws Exception
*/
public static void main(String[] args) throws Exception {
Properties prop = new Properties();
prop.setProperty("mail.host", "smtp.sohu.com");
prop.setProperty("mail.transport.protocol", "smtp");
prop.setProperty("mail.smtp.auth", "true");
//使用JavaMail發(fā)送郵件的5個(gè)步驟
//1、創(chuàng)建session
Session session = Session.getInstance(prop);
//開啟Session的debug模式,這樣就可以查看到程序發(fā)送Email的運(yùn)行狀態(tài)
session.setDebug(true);
//2、通過session得到transport對象
Transport ts = session.getTransport();
//3、使用郵箱的用戶名和密碼連上郵件服務(wù)器,發(fā)送郵件時(shí),發(fā)件人需要提交郵箱的用戶名和密碼給smtp服務(wù)器,用戶名和密碼都通過驗(yàn)證之后才能夠正常發(fā)送郵件給收件人。
ts.connect("smtp.sohu.com", "gacl", "郵箱密碼");
//4、創(chuàng)建郵件
Message message = createSimpleMail(session);
//5、發(fā)送郵件
ts.sendMessage(message, message.getAllRecipients());
ts.close();
}
/**
* @Method: createSimpleMail
* @Description: 創(chuàng)建一封只包含文本的郵件
* @param session
* @return
* @throws Exception
*/
public static MimeMessage createSimpleMail(Session session)
throws Exception {
//創(chuàng)建郵件對象
MimeMessage message = new MimeMessage(session);
//指明郵件的發(fā)件人
message.setFrom(new InternetAddress("gacl@sohu.com"));
//指明郵件的收件人,現(xiàn)在發(fā)件人和收件人是一樣的,那就是自己給自己發(fā)
message.setRecipient(Message.RecipientType.TO, new InternetAddress("gacl@sohu.com"));
//郵件的標(biāo)題
message.setSubject("只包含文本的簡單郵件");
//郵件的文本內(nèi)容
message.setContent("你好啊!", "text/html;charset=UTF-8");
//返回創(chuàng)建好的郵件對象
return message;
}
}
import java.util.*;
import javax.mail.*;import javax.mail.internet.*;
public class JMail {
public void SendMail(String Topic,String Content){ Properties props=new Properties(); props.put("mail.smtp.host","smtp.163.com"); props.put("mail.smtp.auth","true"); Session s=Session.getInstance(props); s.setDebug(false); MimeMessage message=new MimeMessage(s); MimeMultipart mp=new MimeMultipart(); BodyPart body = new MimeBodyPart(); InternetAddress from; InternetAddress to; try{ from=new InternetAddress("發(fā)件人郵箱"); message.setFrom(from); to = new InternetAddress("收件人郵箱"); message.setRecipient(Message.RecipientType.TO,to); message.setSubject(Topic,"utf-8"); body.setContent(Content, "text/html;charset=utf-8"); mp.addBodyPart(body); message.setContent(mp); message.setSentDate(new Date()); message.saveChanges(); Transport transport=s.getTransport("smtp"); transport.connect("smtp.163.com(郵件服務(wù)商,這是163的)","發(fā)件郵箱","發(fā)件郵箱密碼"); transport.sendMessage(message,message.getAllRecipients()); transport.close(); } catch(AddressException e){ e.printStackTrace(); } catch(MessagingException e){ e.printStackTrace(); } }}
import java.util.Properties;
import java.util.Date;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.InternetAddress;
public class HelloWorldJavaMail {
public static void main(String[] args) {
try {
Properties props = System.getProperties();
props.put("mail.smtp.host","xxx.xxx.xxx.xxx");
Session session=Session.getDefaultInstance(props,null);
MimeMessage mimeMessage=new MimeMessage(session);
mimeMessage.setFrom(new InternetAddress("xx@xx.xx","姓名","gb2312"));
mimeMessage.setRecipients(Message.RecipientType.TO,"xx@xx.xx");
mimeMessage.setSubject("Hello World JavaMail","gb2312");
mimeMessage.setText("h1Hello World JavaMail/h1","gb2312");
mimeMessage.setHeader("Content-Type","text/html");
mimeMessage.setSentDate(new Date());
Transport.send(mimeMessage);
} catch (Exception e) {
e.printStackTrace();
}
}
}
2。收信
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class HelloWorldPOP {
public static void main(String args[]) {
try {
String host="xxx.xxx.xxx.xxx";
String user="xxxxxx";
String password="xxxxxx";
Session session = Session.getDefaultInstance(System.getProperties(), null);
Store store = session.getStore("pop3");
store.connect(host, -1, user, password);
Folder folder = store.getFolder("INBOX");
folder.open(Folder.READ_ONLY);
int totalMessages = folder.getMessageCount();
if (totalMessages == 0) {
System.out.println("沒有郵件");
folder.close(false);
store.close();
return;
}
Message[] messages = folder.getMessages();
for (int i = 0; i messages.length; i++) {
showMessage(messages[i]);
}
folder.close(false);
store.close();
} catch (Exception e) {
e.printStackTrace();
}
}
public static void showMessage(Message m) throws Exception {
Address[] a;
if ((a = m.getFrom())!=null) {
for (int j = 0; j a.length; j++) {
System.out.println("送信人: " + MimeUtility.decodeText(a[j].toString()));
}
}
if ((a = m.getRecipients(Message.RecipientType.TO))!=null) {
for (int j = 0; j a.length; j++) {
System.out.println("收信人: " + MimeUtility.decodeText(a[j].toString()));
}
}
System.out.println("主體: " + m.getSubject());
Date d = m.getSentDate();
System.out.println("日期: " + (d!= null ? d.toString() : "不明"));
System.out.println("大小: " + m.getSize());
System.out.println("內(nèi)容:\n" + m.getContent());
}
}
用JAVAMAIL很容易實(shí)現(xiàn)的。
參照:
public boolean mainto()
{
boolean flag = true;
//建立郵件會(huì)話
Properties pro = new Properties();
pro.put("mail.smtp.host","smtp.qq.com");//存儲(chǔ)發(fā)送郵件的服務(wù)器
pro.put("mail.smtp.auth","true"); //通過服務(wù)器驗(yàn)證
Session s =Session.getInstance(pro); //根據(jù)屬性新建一個(gè)郵件會(huì)話
//s.setDebug(true);
//由郵件會(huì)話新建一個(gè)消息對象
MimeMessage message = new MimeMessage(s);
//設(shè)置郵件
InternetAddress fromAddr = null;
InternetAddress toAddr = null;
try
{
fromAddr = new InternetAddress(451144426+"@qq.com"); //郵件發(fā)送地址
message.setFrom(fromAddr); //設(shè)置發(fā)送地址
toAddr = new InternetAddress("12345367@qq.com"); //郵件接收地址
message.setRecipient(Message.RecipientType.TO, toAddr); //設(shè)置接收地址
message.setSubject(title); //設(shè)置郵件標(biāo)題
message.setText(content); //設(shè)置郵件正文
message.setSentDate(new Date()); //設(shè)置郵件日期
message.saveChanges(); //保存郵件更改信息
Transport transport = s.getTransport("smtp");
transport.connect("smtp.qq.com", "451144426", "密碼"); //服務(wù)器地址,郵箱賬號,郵箱密碼
transport.sendMessage(message, message.getAllRecipients()); //發(fā)送郵件
transport.close();//關(guān)閉
}
catch (Exception e)
{
e.printStackTrace();
flag = false;//發(fā)送失敗
}
return flag;
}
這是一個(gè)javaMail的郵件發(fā)送代碼,需要一個(gè)mail.jar