最近2018中文字幕在日韩欧美国产成人片_国产日韩精品一区二区在线_在线观看成年美女黄网色视频_国产精品一区三区五区_国产精彩刺激乱对白_看黄色黄大色黄片免费_人人超碰自拍cao_国产高清av在线_亚洲精品电影av_日韩美女尤物视频网站

RELATEED CONSULTING
相關(guān)咨詢
選擇下列產(chǎn)品馬上在線溝通
服務(wù)時間:8:30-17:00
你可能遇到了下面的問題
關(guān)閉右側(cè)工具欄

新聞中心

這里有您想知道的互聯(lián)網(wǎng)營銷解決方案
通過java代碼發(fā)郵件 java實現(xiàn)郵件發(fā)送功能代碼

怎么用java發(fā)送郵件,像園子那樣

1.首先你需要一個郵箱中轉(zhuǎn)發(fā)送站(聽著很高端的樣子),說白了就是注冊一個郵箱作為你的發(fā)送郵件平臺,然后通過編程調(diào)用平臺發(fā)送郵件(也就是你注冊某個郵箱,然后開通SMTP/POP3協(xié)議,在編程中,拿著你的KEY去發(fā)送郵件),我試過很多種郵箱,QQ貌似不能用,網(wǎng)易經(jīng)常報錯,建議用新浪的,我用基本沒出過問題。

目前創(chuàng)新互聯(lián)已為上千余家的企業(yè)提供了網(wǎng)站建設(shè)、域名、虛擬空間、網(wǎng)站托管、服務(wù)器托管、企業(yè)網(wǎng)站設(shè)計、石棉網(wǎng)站維護等服務(wù),公司將堅持客戶導向、應(yīng)用為本的策略,正道將秉承"和諧、參與、激情"的文化,與客戶和合作伙伴齊心協(xié)力一起成長,共同發(fā)展。

2.去網(wǎng)上下載java開源的發(fā)送郵件工具類:mail.jar,并導入myeclipse/eclipse的引用。

3.編程(工具類)

java 怎么實現(xiàn)發(fā)送郵件例子

第一個類:MailSenderInfo.java

[java] view plain copy

package com.util.mail;

/**

* 發(fā)送郵件需要使用的基本信息

*author by wangfun

小說520

*/

import java.util.Properties;

public class MailSenderInfo {

// 發(fā)送郵件的服務(wù)器的IP和端口

private String mailServerHost;

private String mailServerPort = "25";

// 郵件發(fā)送者的地址

private String fromAddress;

// 郵件接收者的地址

private String toAddress;

// 登陸郵件發(fā)送服務(wù)器的用戶名和密碼

private String userName;

private String password;

// 是否需要身份驗證

private boolean validate = false;

// 郵件主題

private String subject;

// 郵件的文本內(nèi)容

private String content;

// 郵件附件的文件名

private String[] attachFileNames;

/**

* 獲得郵件會話屬性

*/

public Properties getProperties(){

Properties p = new Properties();

p.put("mail.smtp.host", this.mailServerHost);

p.put("mail.smtp.port", this.mailServerPort);

p.put("mail.smtp.auth", validate ? "true" : "false");

return p;

}

public String getMailServerHost() {

return mailServerHost;

}

public void setMailServerHost(String mailServerHost) {

this.mailServerHost = mailServerHost;

}

public String getMailServerPort() {

return mailServerPort;

}

public void setMailServerPort(String mailServerPort) {

this.mailServerPort = mailServerPort;

}

public boolean isValidate() {

return validate;

}

public void setValidate(boolean validate) {

this.validate = validate;

}

public String[] getAttachFileNames() {

return attachFileNames;

}

public void setAttachFileNames(String[] fileNames) {

this.attachFileNames = fileNames;

}

public String getFromAddress() {

return fromAddress;

}

public void setFromAddress(String fromAddress) {

this.fromAddress = fromAddress;

}

public String getPassword() {

return password;

}

public void setPassword(String password) {

this.password = password;

}

public String getToAddress() {

return toAddress;

}

public void setToAddress(String toAddress) {

this.toAddress = toAddress;

}

public String getUserName() {

return userName;

}

public void setUserName(String userName) {

this.userName = userName;

}

public String getSubject() {

return subject;

}

public void setSubject(String subject) {

this.subject = subject;

}

public String getContent() {

return content;

}

public void setContent(String textContent) {

this.content = textContent;

}

}

第二個類:SimpleMailSender.java

[java] view plain copy

package com.util.mail;

import java.util.Date;

import java.util.Properties;

import javax.mail.Address;

import javax.mail.BodyPart;

import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Multipart;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeBodyPart;

import javax.mail.internet.MimeMessage;

import javax.mail.internet.MimeMultipart;

/**

* 簡單郵件(不帶附件的郵件)發(fā)送器

BT下載

*/

public class SimpleMailSender {

/**

* 以文本格式發(fā)送郵件

* @param mailInfo 待發(fā)送的郵件的信息

*/

public boolean sendTextMail(MailSenderInfo mailInfo) {

// 判斷是否需要身份認證

MyAuthenticator authenticator = null;

Properties pro = mailInfo.getProperties();

if (mailInfo.isValidate()) {

// 如果需要身份認證,則創(chuàng)建一個密碼驗證器

authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());

}

// 根據(jù)郵件會話屬性和密碼驗證器構(gòu)造一個發(fā)送郵件的session

Session sendMailSession = Session.getDefaultInstance(pro,authenticator);

try {

// 根據(jù)session創(chuàng)建一個郵件消息

Message mailMessage = new MimeMessage(sendMailSession);

// 創(chuàng)建郵件發(fā)送者地址

Address from = new InternetAddress(mailInfo.getFromAddress());

// 設(shè)置郵件消息的發(fā)送者

mailMessage.setFrom(from);

// 創(chuàng)建郵件的接收者地址,并設(shè)置到郵件消息中

Address to = new InternetAddress(mailInfo.getToAddress());

mailMessage.setRecipient(Message.RecipientType.TO,to);

// 設(shè)置郵件消息的主題

mailMessage.setSubject(mailInfo.getSubject());

// 設(shè)置郵件消息發(fā)送的時間

mailMessage.setSentDate(new Date());

// 設(shè)置郵件消息的主要內(nèi)容

String mailContent = mailInfo.getContent();

mailMessage.setText(mailContent);

// 發(fā)送郵件

Transport.send(mailMessage);

return true;

} catch (MessagingException ex) {

ex.printStackTrace();

}

return false;

}

/**

* 以HTML格式發(fā)送郵件

* @param mailInfo 待發(fā)送的郵件信息

*/

public static boolean sendHtmlMail(MailSenderInfo mailInfo){

// 判斷是否需要身份認證

MyAuthenticator authenticator = null;

Properties pro = mailInfo.getProperties();

//如果需要身份認證,則創(chuàng)建一個密碼驗證器

if (mailInfo.isValidate()) {

authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword());

}

// 根據(jù)郵件會話屬性和密碼驗證器構(gòu)造一個發(fā)送郵件的session

Session sendMailSession = Session.getDefaultInstance(pro,authenticator);

try {

// 根據(jù)session創(chuàng)建一個郵件消息

Message mailMessage = new MimeMessage(sendMailSession);

// 創(chuàng)建郵件發(fā)送者地址

Address from = new InternetAddress(mailInfo.getFromAddress());

// 設(shè)置郵件消息的發(fā)送者

mailMessage.setFrom(from);

// 創(chuàng)建郵件的接收者地址,并設(shè)置到郵件消息中

Address to = new InternetAddress(mailInfo.getToAddress());

// Message.RecipientType.TO屬性表示接收者的類型為TO

mailMessage.setRecipient(Message.RecipientType.TO,to);

// 設(shè)置郵件消息的主題

mailMessage.setSubject(mailInfo.getSubject());

// 設(shè)置郵件消息發(fā)送的時間

mailMessage.setSentDate(new Date());

// MiniMultipart類是一個容器類,包含MimeBodyPart類型的對象

Multipart mainPart = new MimeMultipart();

// 創(chuàng)建一個包含HTML內(nèi)容的MimeBodyPart

BodyPart html = new MimeBodyPart();

// 設(shè)置HTML內(nèi)容

html.setContent(mailInfo.getContent(), "text/html; charset=utf-8");

mainPart.addBodyPart(html);

// 將MiniMultipart對象設(shè)置為郵件內(nèi)容

mailMessage.setContent(mainPart);

// 發(fā)送郵件

Transport.send(mailMessage);

return true;

} catch (MessagingException ex) {

ex.printStackTrace();

}

return false;

}

}

第三個類:MyAuthenticator.java

[java] view plain copy

package com.util.mail;

import javax.mail.*;

public class MyAuthenticator extends Authenticator{

String userName=null;

String password=null;

public MyAuthenticator(){

}

public MyAuthenticator(String username, String password) {

this.userName = username;

this.password = password;

}

protected PasswordAuthentication getPasswordAuthentication(){

return new PasswordAuthentication(userName, password);

}

}

下面給出使用上面三個類的代碼:

[java] view plain copy

public static void main(String[] args){

//這個類主要是設(shè)置郵件

MailSenderInfo mailInfo = new MailSenderInfo();

mailInfo.setMailServerHost("smtp.163.com");

mailInfo.setMailServerPort("25");

mailInfo.setValidate(true);

mailInfo.setUserName("[email protected]");

mailInfo.setPassword("**********");//您的郵箱密碼

mailInfo.setFromAddress("[email protected]");

mailInfo.setToAddress("[email protected]");

mailInfo.setSubject("設(shè)置郵箱標題 如 中國桂花網(wǎng)");

mailInfo.setContent("設(shè)置郵箱內(nèi)容 如 中國桂花網(wǎng) 是中國最大桂花網(wǎng)站==");

//這個類主要來發(fā)送郵件

SimpleMailSender sms = new SimpleMailSender();

sms.sendTextMail(mailInfo);//發(fā)送文體格式

sms.sendHtmlMail(mailInfo);//發(fā)送html格式

}

最后,給出朋友們幾個注意的地方:

1、使用此代碼你可以完成你的javamail的郵件發(fā)送功能。三個類缺一不可。

2、這三個類我打包是用的com.util.mail包,如果不喜歡,你可以自己改,但三個類文件必須在同一個包中

3、不要使用你剛剛注冊過的郵箱在程序中發(fā)郵件,如果你的163郵箱是剛注冊不久,那你就不要使用“smtp.163.com”。因為你發(fā)不出去。剛注冊的郵箱是不會給你這種權(quán)限的,也就是你不能通過驗證。要使用你經(jīng)常用的郵箱,而且時間比較長的。

4、另一個問題就是mailInfo.setMailServerHost("smtp.163.com");與mailInfo.setFromAddress("[email protected]");這兩句話。即如果你使用163smtp服務(wù)器,那么發(fā)送郵件地址就必須用163的郵箱,如果不的話,是不會發(fā)送成功的。

5、關(guān)于javamail驗證錯誤的問題,網(wǎng)上的解釋有很多,但我看見的只有一個。就是我的第三個類。你只要復制全了代碼,我想是不會有問題的。

怎樣用java實現(xiàn)郵件的發(fā)送?

import java.io.BufferedReader;

import java.io.BufferedWriter;

import java.io.IOException;

import java.io.InputStreamReader;

import java.io.OutputStreamWriter;

import java.net.Socket;

import java.net.SocketException;

import java.rmi.UnknownHostException;

import java.util.StringTokenizer;

import sun.misc.BASE64Encoder;

public class Sender {

//private boolean debug = true;

BASE64Encoder encode=new BASE64Encoder();//用于加密后發(fā)送用戶名和密碼

static int dk=25;

private Socket socket;

public Sender(String server, int port) throws UnknownHostException,

IOException {

try {

socket = new Socket(server, dk);

} catch (SocketException e) {

System.out.println(e.getMessage());

} catch (Exception e) {

e.printStackTrace();

} finally {

//System.out.println("已經(jīng)建立連接!");

}

}

// 注冊到郵件服務(wù)器

public void helo(String server, BufferedReader in, BufferedWriter out)

throws IOException {

int result;

result = getResult(in);

// 連接上郵件服務(wù)后,服務(wù)器給出220應(yīng)答

if (result != 220) {

throw new IOException("連接服務(wù)器失敗");

}

result = sendServer("HELO " + server, in, out);

// HELO命令成功后返回250

if (result != 250) {

throw new IOException("注冊郵件服務(wù)器失??!");

}

}

private int sendServer(String str, BufferedReader in, BufferedWriter out)

throws IOException {

out.write(str);

out.newLine();

out.flush();

/*

if (debug) {

System.out.println("已發(fā)送命令:" + str);

}

*/

return getResult(in);

}

public int getResult(BufferedReader in) {

String line = "";

try {

line = in.readLine();

/*

if (debug) {

System.out.println("服務(wù)器返回狀態(tài):" + line);

}

*/

} catch (Exception e) {

e.printStackTrace();

}

// 從服務(wù)器返回消息中讀出狀態(tài)碼,將其轉(zhuǎn)換成整數(shù)返回

StringTokenizer st = new StringTokenizer(line, " ");

return Integer.parseInt(st.nextToken());

}

public void authLogin(MailMessage message, BufferedReader in,

BufferedWriter out) throws IOException {

int result;

result = sendServer("AUTH LOGIN", in, out);

if (result != 334) {

throw new IOException("用戶驗證失敗!");

}

result=sendServer(encode.encode(message.getUser().getBytes()),in,out);

//System.out.println("用戶名: "+encode.encode(message.getUser().getBytes()));

if (result != 334) {

throw new IOException("用戶名錯誤!");

}

result=sendServer(encode.encode(message.getPassword().getBytes()),in,out);

//result=sendServer(message.getPassword(),in,out);

//System.out.println("密碼: "+encode.encode(message.getPassword().getBytes()));

if (result != 235) {

throw new IOException("驗證失?。?);

}

}

// 開始發(fā)送消息,郵件源地址

public void mailfrom(String source, BufferedReader in, BufferedWriter out)

throws IOException {

int result;

result = sendServer("MAIL FROM:" + source + "", in, out);

if (result != 250) {

throw new IOException("指定源地址錯誤");

}

}

// 設(shè)置郵件收件人

public void rcpt(String touchman, BufferedReader in, BufferedWriter out)

throws IOException {

int result;

result = sendServer("RCPT TO:" + touchman + "", in, out);

if (result != 250) {

throw new IOException("指定目的地址錯誤!");

}

}

// 郵件體

public void data(String from, String to, String subject, String content,

BufferedReader in, BufferedWriter out) throws IOException {

int result;

result = sendServer("DATA", in, out);

// 輸入DATA回車后,若收到354應(yīng)答后,繼續(xù)輸入郵件內(nèi)容

if (result != 354) {

throw new IOException("不能發(fā)送數(shù)據(jù)");

}

out.write("From: " + from);

out.newLine();

out.write("To: " + to);

out.newLine();

out.write("Subject: " + subject);

out.newLine();

out.newLine();

out.write(content);

out.newLine();

// 句號加回車結(jié)束郵件內(nèi)容輸入

result = sendServer(".", in, out);

//System.out.println(result);

if (result != 250) {

throw new IOException("發(fā)送數(shù)據(jù)錯誤");

}

}

// 退出

public void quit(BufferedReader in, BufferedWriter out) throws IOException {

int result;

result = sendServer("QUIT", in, out);

if (result != 221) {

throw new IOException("未能正確退出");

}

}

// 發(fā)送郵件主程序

public boolean sendMail(MailMessage message, String server) {

try {

BufferedReader in = new BufferedReader(new InputStreamReader(

socket.getInputStream()));

BufferedWriter out = new BufferedWriter(new OutputStreamWriter(

socket.getOutputStream()));

helo(server, in, out);// HELO命令

authLogin(message, in, out);// AUTH LOGIN命令

mailfrom(message.getFrom(), in, out);// MAIL FROM

rcpt(message.getTo(), in, out);// RCPT

data(message.getDatafrom(), message.getDatato(),

message.getSubject(), message.getContent(), in, out);// DATA

quit(in, out);// QUIT

} catch (Exception e) {

e.printStackTrace();

return false;

}

return true;

}

}

再寫一個MailMessage.java,set/get方法即可。

如何使用Java發(fā)送qq郵件

方法:

1.前提準備工作:

首先,郵件的發(fā)送方要開啟POP3 和SMTP服務(wù)--即發(fā)送qq郵件的賬號要開啟POP3 和SMTP服務(wù)

2.開啟方法:

登陸qq郵箱

3.點擊 設(shè)置

4.點擊—-賬戶

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

6.送短信 —–點擊確定

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

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");// 連接協(xié)議

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");//設(shè)置是否使用ssl安全連接 ---一般都使用

properties.put("mail.debug", "true");//設(shè)置是否顯示debug信息 true 會在控制臺顯示相關(guān)信息

//得到回話對象

Session session = Session.getInstance(properties);

// 獲取郵件對象

Message message = new MimeMessage(session);

//設(shè)置發(fā)件人郵箱地址

message.setFrom(new InternetAddress("[email protected]"));

//設(shè)置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("[email protected]") });

//設(shè)置郵件標題

message.setSubject("這是第一封Java郵件");

//設(shè)置郵件內(nèi)容

message.setText("內(nèi)容為: 這是第一封java發(fā)送來的郵件。");

//得到郵差對象

Transport transport = session.getTransport();

//連接自己的郵箱賬戶

transport.connect("[email protected]", "vvctybgbvvophjcj");//密碼為剛才得到的授權(quán)碼

//發(fā)送郵件 transport.sendMessage(message, message.getAllRecipients());

}

}

10.運行就會發(fā)出郵件了。。。。

下面是我收到郵件的截圖,當然我把源碼中的郵件地址都是修改了,不是真實的,你們測試的時候,可以修改能你們自己的郵箱。最后,祝你也能成功,如果有什么問題,可以一起討論!

注意事項

得到的授權(quán)碼一定要保存好,程序中要使用


網(wǎng)站標題:通過java代碼發(fā)郵件 java實現(xiàn)郵件發(fā)送功能代碼
URL鏈接:http://fisionsoft.com.cn/article/hhocsh.html