導航:首頁 > 編程語言 > java郵件發送系統

java郵件發送系統

發布時間:2023-06-09 19:13:50

『壹』 用java寫一個郵件發送代碼

public boolean mainto()
{
boolean flag = true;

//建立郵件會話
Properties pro = new Properties();
pro.put("mail.smtp.host","smtp.qq.com");//存儲發送郵件的伺服器
pro.put("mail.smtp.auth","true"); //通過伺服器驗證

Session s =Session.getInstance(pro); //根據屬性新建一個郵件會話
//s.setDebug(true);

//由郵件會話新建一個消息對象
MimeMessage message = new MimeMessage(s);

//設置郵件
InternetAddress fromAddr = null;
InternetAddress toAddr = null;

try
{
fromAddr = new InternetAddress(451144426+"@qq.com"); //郵件發送地址
message.setFrom(fromAddr); //設置發送地址

toAddr = new InternetAddress("[email protected]"); //郵件接收地址
message.setRecipient(Message.RecipientType.TO, toAddr); //設置接收地址

message.setSubject(title); //設置郵件標題
message.setText(content); //設置郵件正文
message.setSentDate(new Date()); //設置郵件日期

message.saveChanges(); //保存郵件更改信息

Transport transport = s.getTransport("smtp");
transport.connect("smtp.qq.com", "451144426", "密碼"); //伺服器地址,郵箱賬號,郵箱密碼
transport.sendMessage(message, message.getAllRecipients()); //發送郵件
transport.close();//關閉

}
catch (Exception e)
{
e.printStackTrace();
flag = false;//發送失敗
}

return flag;
}

這是一個javaMail的郵件發送代碼,需要一個mail.jar

『貳』 怎樣用java實現郵件的發送

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();//用於加密後發送用戶名和密碼
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("已經建立連接!");
}

}

// 注冊到郵件伺服器
public void helo(String server, BufferedReader in, BufferedWriter out)
throws IOException {
int result;
result = getResult(in);
// 連接上郵件服務後,伺服器給出220應答
if (result != 220) {
throw new IOException("連接伺服器失敗");
}
result = sendServer("HELO " + server, in, out);
// HELO命令成功後返回250
if (result != 250) {
throw new IOException("注冊郵件伺服器失敗!");
}
}

private int sendServer(String str, BufferedReader in, BufferedWriter out)
throws IOException {
out.write(str);
out.newLine();
out.flush();
/*
if (debug) {
System.out.println("已發送命令:" + str);
}
*/
return getResult(in);
}

public int getResult(BufferedReader in) {
String line = "";
try {
line = in.readLine();
/*
if (debug) {
System.out.println("伺服器返回狀態:" + line);
}
*/
} catch (Exception e) {
e.printStackTrace();
}
// 從伺服器返回消息中讀出狀態碼,將其轉換成整數返回

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("驗證失敗!");
}
}

// 開始發送消息,郵件源地址
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("指定源地址錯誤");
}
}

// 設置郵件收件人
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應答後,繼續輸入郵件內容
if (result != 354) {
throw new IOException("不能發送數據");
}
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();
// 句號加回車結束郵件內容輸入
result = sendServer(".", in, out);
//System.out.println(result);
if (result != 250) {
throw new IOException("發送數據錯誤");
}
}

// 退出
public void quit(BufferedReader in, BufferedWriter out) throws IOException {
int result;
result = sendServer("QUIT", in, out);
if (result != 221) {
throw new IOException("未能正確退出");
}
}

// 發送郵件主程序
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實現郵件發送的源代碼

需要用到javamail的jar包,網上有。找不到把郵箱貼出來,我發給你。 package test.servlet;import java.io.IOException;
import java.io.PrintWriter;
import java.util.Date;
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.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;public class SendMailServlet extends HttpServlet {
/**
*
*/
private static final long serialVersionUID = 2960000940515881314L;
private ServletConfig config = null;
private static final String CONTENT_TYPE = "text/html"; public void init(ServletConfig config1) throws ServletException {
this.config = config1;
} final public ServletConfig getServletConfig() {
return config;
} /**
* Constructor of the object.
*/
public SendMailServlet() {
super();
} /**
* Destruction of the servlet.
*/
public void destroy() {
super.destroy();
} /**
* The doGet method of the servlet.
*
* This method is called when a form has its tag value method equals to get.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
doPost(request,response);
} /**
* The doPost method of the servlet.
*
* This method is called when a form has its tag value method equals to post.
*
* @param request the request send by the client to the server
* @param response the response send by the server to the client
* @throws ServletException if an error occurred
* @throws IOException if an error occurred
*/
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {

String to_mail=codeToString(request.getParameter("to"));
String to_title=codeToString(request.getParameter("title"));
String to_content=codeToString(request.getParameter("content"));

Properties props=new Properties();//也可用Properties props = System.getProperties(); //
props.put("mail.smtp.host","localhost");//存儲發送郵件伺服器的信息
props.put("mail.smtp.auth","true");//同時通過驗證
Session s=Session.getInstance(props);//根據屬性新建一個郵件會話
s.setDebug(true); //由郵件會話新建一個消息對象
MimeMessage message=new MimeMessage(s);//由郵件會話新建一個消息對象 //設置郵件
InternetAddress from;
try {
from = new InternetAddress("[email protected]");
message.setFrom(from);//設置發件人
InternetAddress to=new InternetAddress(to_mail);
message.setRecipient(Message.RecipientType.TO,to);//設置收件人,並設置其接收類型為TO
message.setSubject(to_title);//設置主題
message.setText(to_content);//設置信件內容
message.setSentDate(new Date());//設置發信時間
//發送郵件
message.saveChanges();//存儲郵件信息
Transport transport=s.getTransport("smtp");
//以smtp方式登錄郵箱,第一個參數是發送郵件用的郵件伺服器SMTP地址,第二個參數為用戶名,第三個參數為密碼
transport.connect("localhost",8479,"","");
transport.sendMessage(message,message.getAllRecipients());//發送郵件,其中第二個參數是所有已設好的收件人地址
transport.close();
response.setContentType(CONTENT_TYPE);
PrintWriter out = response.getWriter();
out.print("success");
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
} }

public String codeToString(String str) {//處理中文字元串的函數
String s=str;
try {
byte tempB[]=s.getBytes("ISO-8859-1");
s=new String(tempB);
return s;
} catch(Exception e) {
return s;
}
}
}

『肆』 java實現發送郵件功能

要實現郵件發送功能需要導入包:mail.jar

/*
* Generated by MyEclipse Struts
* Template path: templates/java/JavaClass.vtl
*/
package org.demo.action;

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;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.demo.form.DemoForm;

public class DemoAction extends Action {

private static final String CONTENT_TYPE = "test/html;charset=GB2312";

public ActionForward execute(ActionMapping mapping, ActionForm form,
HttpServletRequest request, HttpServletResponse response) {
DemoForm demoForm = (DemoForm) form;
System.out.println("標題是" + demoForm.getBiaoti());
System.out.println("內容是" + demoForm.getNeirong());
try {
response.setContentType(CONTENT_TYPE);
String smtphost = "smtp.nj.headware.cn"; // 發送郵件伺服器
String user = "q0000015369"; // 郵件伺服器登錄用戶名
String password = "Queshuwen26"; // 郵件伺服器登錄密碼
String from = "[email protected]"; //
String to = "[email protected]"; // 收件人郵件地址
String subject = demoForm.getBiaoti(); // 郵件標題
String body = demoForm.getNeirong(); // 郵件內容
Properties props = new Properties();
props.put("mail.smtp.host", smtphost);
props.put("mail.smtp.auth", "true");
Session ssn = Session.getInstance(props, null);

MimeMessage message = new MimeMessage(ssn);

InternetAddress fromAddress = new InternetAddress(from);
message.setFrom(fromAddress);
InternetAddress toAddress = new InternetAddress(to);
message.addRecipient(Message.RecipientType.TO, toAddress);
message.setSubject(subject);
message.setText(body);
Transport transport = ssn.getTransport("smtp");

transport.connect(smtphost, user, password);

transport.sendMessage(message, message
.getRecipients(Message.RecipientType.TO));
// transport.send(message);
transport.close();
return mapping.findForward("succ");
} catch (Exception e) {
e.printStackTrace();
return mapping.findForward("fail");
}

}
}

『伍』 java 發送郵件

要兩個java文件 還有一個mail.jar是不是只能用javamail誰也不敢說

第一個:

public class Constant {

public static final String mailAddress ="用戶名@163.com";
public static final String mailCount ="用戶名";
public static final String mailPassword ="密碼";
public static final String mailServer ="smtp.163.com";
//pukeyouxintest,

}

第二個:

import java.util.Date;
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 str_from:發件人地址
* @param str_to:收件人地址
* @param str_title:郵件標題
* @param str_content:郵件正文
*/
public static void send(String str_from,String str_to,String str_title,String str_content) {

// str_content="<a href='www.163.com'>html元素</a>"; //for testing send html mail!

try {
//建立郵件會話
Properties props=new Properties(); //用來在一個文件中存儲鍵-值對的,其中鍵和值是用等號分隔的,
//存儲發送郵件伺服器的信息
props.put("mail.smtp.host",Constant.mailServer);
//同時通過驗證
props.put("mail.smtp.auth","true");
//根據屬性新建一個郵件會話
Session s=Session.getInstance(props);
s.setDebug(true); //有他會列印一些調試信息。

//由郵件會話新建一個消息對象
MimeMessage message=new MimeMessage(s);

//設置郵件
InternetAddress from= new InternetAddress(str_from); //[email protected]
message.setFrom(from); //設置發件人的地址
//
// //設置收件人,並設置其接收類型為TO
InternetAddress to=new InternetAddress(str_to); //[email protected]
message.setRecipient(Message.RecipientType.TO, to);

//設置標題
message.setSubject(str_title); //java學習

//設置信件內容
// message.setText(str_content); //發送文本郵件 //你好嗎?
message.setContent(str_content, "text/html;charset=gbk"); //發送HTML郵件 //<b>你好</b><br><p>大家好</p>
//設置發信時間
message.setSentDate(new Date());

//存儲郵件信息
message.saveChanges();

//發送郵件
Transport transport=s.getTransport("smtp");
//以smtp方式登錄郵箱,第一個參數是發送郵件用的郵件伺服器SMTP地址,第二個參數為用戶名,第三個參數為密碼
transport.connect(Constant.mailServer,Constant.mailCount,Constant.mailPassword);
//發送郵件,其中第二個參數是所有已設好的收件人地址
transport.sendMessage(message,message.getAllRecipients());
transport.close();

} catch (Exception e) {
e.printStackTrace();
}
}

public static void main(String[] args) {
//測試用的,你吧你想寫的內容寫上去就行
send(Constant.mailAddress,"收件人郵箱","標題","<b>內容</b>");
}
}

然後把mail.jar導入,就可以了,我用的是163 的,其他的吧相應的伺服器改一下就行了

『陸』 如何使用Java發送qq郵件

方法:

1.前提准備工作:
首先,郵件的發送方要開啟POP3 和SMTP服務--即發送qq郵件的賬號要開啟POP3 和SMTP服務

2.開啟方法:
登陸qq郵箱
3.點擊 設置

4.點擊—-賬戶

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

6.送簡訊 —–點擊確定

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

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");// 連接協議
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");//設置是否使用ssl安全連接 ---一般都使用
properties.put("mail.debug", "true");//設置是否顯示debug信息 true 會在控制台顯示相關信息
//得到回話對象
Session session = Session.getInstance(properties);
// 獲取郵件對象
Message message = new MimeMessage(session);
//設置發件人郵箱地址
message.setFrom(new InternetAddress("[email protected]"));
//設置收件人地址 message.setRecipients( RecipientType.TO, new InternetAddress[] { new InternetAddress("[email protected]") });
//設置郵件標題
message.setSubject("這是第一封Java郵件");
//設置郵件內容
message.setText("內容為: 這是第一封java發送來的郵件。");
//得到郵差對象
Transport transport = session.getTransport();
//連接自己的郵箱賬戶
transport.connect("[email protected]", "vvctybgbvvophjcj");//密碼為剛才得到的授權碼
//發送郵件 transport.sendMessage(message, message.getAllRecipients());
}
}
10.運行就會發出郵件了。。。。
下面是我收到郵件的截圖,當然我把源碼中的郵件地址都是修改了,不是真實的,你們測試的時候,可以修改能你們自己的郵箱。最後,祝你也能成功,如果有什麼問題,可以一起討論!

注意事項

得到的授權碼一定要保存好,程序中要使用

『柒』 怎麼用JAVA實現郵件發送

一個小例子,也可使用其他api

importjava.util.Properties;
importjavax.mail.Address;
importjavax.mail.Authenticator;
importjavax.mail.BodyPart;
importjavax.mail.Message;
importjavax.mail.Multipart;
importjavax.mail.PasswordAuthentication;
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;

publicclassTest{

//test
publicstaticvoidmain(String[]args)throwsException{
sendEmail("smtp.163.com","測試","測試","!!!收件人地址!!!","!!!發件人郵箱用戶名!!!","!!!郵箱密碼!!!","發件人昵稱");
}

/**
*
*@paramsmtp
*@throwsException
*/
publicstaticvoidsendEmail(StringemailServer,Stringsubject,StringmailBody,Stringreceiver,finalStringusername,finalStringpassword,Stringnickname)throwsException{
Propertiesprops=newProperties();
props.put("mail.smtp.auth","true");
props.setProperty("mail.transport.protocol","smtp");
props.setProperty("mail.smtp.host",emailServer);
props.setProperty("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.setProperty("mail.smtp.socketFactory.fallback","false");
props.setProperty("mail.smtp.port","465");
props.setProperty("mail.smtp.socketFactory.port","465");
Sessionsession=Session.getDefaultInstance(props,newAuthenticator(){
(){
(username,password);
}
});
session.setDebug(true);
MimeMessagemimeMsg=newMimeMessage(session);
Multipartmp=newMimeMultipart();

mimeMsg.setSubject(MimeUtility.encodeText(subject,"utf-8",null));

nickname=MimeUtility.encodeText(nickname,"utf-8",null);
mimeMsg.setFrom(newInternetAddress(username,nickname,"UTF-8"));

BodyPartbp=newMimeBodyPart();
bp.setContent(mailBody,"text/html;charset=utf-8");
mp.addBodyPart(bp);

mimeMsg.setRecipients(Message.RecipientType.TO,InternetAddress.parse(receiver));

mimeMsg.setContent(mp);
mimeMsg.saveChanges();
Transporttransport=session.getTransport();
transport.connect(username,password);
Address[]allRecipients=mimeMsg.getAllRecipients();
transport.sendMessage(mimeMsg,allRecipients);
transport.close();
}
}

『捌』 用java寫的郵件發送程序

界面自己寫一下就可以了,把相關的參數傳進去就可以了。 這個是我以前寫的。用的javamail。 有main方法,測試一下自己的郵件,應該沒問題的。希望可以幫到你。注意導入你需要的javamail.jar的包 -------------------------------------------------------------- package com.fourpane.mail; import java.util.Properties; import javax.mail.Address; import javax.mail.Flags; import javax.mail.Folder; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.NoSuchProviderException; import javax.mail.Session; import javax.mail.Store; import javax.mail.Transport; import javax.mail.internet.AddressException; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; public class TestMail { public static void main(String[] args) { //TestMail.sendMail(); //TestMail.receiveMail(); TestMail.deleteMail(); } /** * send mail */ public static void sendMail() { String host = "smtp.sina.com";//郵件伺服器 String from = "[email protected]";//發件人地址 String to = "[email protected]";//接受地址(必須支持pop3協議) String userName = "xingui5624";//發件人郵件名稱 String pwd = "******";//發件人郵件密碼 Properties props = new Properties(); props.put("mail.smtp.host", host); props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props); session.setDebug(true); MimeMessage msg = new MimeMessage(session); try { msg.setFrom(new InternetAddress(from)); msg.addRecipient(Message.RecipientType.TO, new InternetAddress(to));//發送 msg.setSubject("我的測試...........");//郵件主題 msg.setText("測試內容。。。。。。。");//郵件內容 msg.saveChanges(); Transport transport = session.getTransport("smtp"); transport.connect(host, userName, pwd);//郵件伺服器驗證 transport.sendMessage(msg, msg.getRecipients(Message.RecipientType.TO)); System.out.println("send ok..........................."); } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } } /** * receive mail */ public static void receiveMail() { String host = "pop3.sina.com"; String userName = "xingui5624"; String passWord = "******"; Properties props = new Properties(); Session session = Session.getDefaultInstance(props); session.setDebug(true); try { System.out.println("receive..............................."); Store store = session.getStore("pop3"); store.connect(host, userName,passWord);//驗證 Folder folder = store.getFolder("INBOX");//取得收件文件夾 folder.open(Folder.READ_WRITE); Message msg[] = folder.getMessages(); System.out.println("郵件個數:" + msg.length); for(int i=0; i<msg.length; i++) { Message message = msg[i]; Address address[] = message.getFrom(); StringBuffer from = new StringBuffer(); /** * 此for循環是我項目測試用的 */ for(int j=0; j<address.length; j++) { if (j > 0) from.append(";"); from.append(address[j].toString()); } System.out.println(message.getMessageNumber()); System.out.println("來自:" + from.toString()); System.out.println("大小:" + message.getSize()); System.out.println("主題:" + message.getSubject()); System.out.println("時間::" + message.getSentDate()); System.out.println("==================================================="); } folder.close(true);//設置關閉 store.close(); System.out.println("receive over............................"); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } } /** * delete mail */ public static void deleteMail() { String host = "pop3.sina.com"; String userName = "xingui5624"; String passWord = "******"; Properties props = new Properties(); //Properties props = System.getProperties();這種方法創建 Porperties 同上 Session session = Session.getDefaultInstance(props); session.setDebug(true); try { System.out.println("begin delete ..........."); Store store = session.getStore("pop3"); store.connect(host, userName, passWord);//驗證郵箱 Folder folder = store.getFolder("INBOX"); folder.open(Folder.READ_WRITE);//設置我讀寫方式打開 int countOfAll = folder.getMessageCount();//取得郵件個數 int unReadCount = folder.getUnreadMessageCount();//已讀個數 int newOfCount = folder.getNewMessageCount();//未讀個數 System.out.println("總個數:" +countOfAll); System.out.println("已讀個數:" +unReadCount); System.out.println("未讀個數:" +newOfCount); for(int i=1; i<=countOfAll; i++) { Message message = folder.getMessage(i); message.setFlag(Flags.Flag.DELETED, true);//設置已刪除狀態為true if(message.isSet(Flags.Flag.DELETED)) System.out.println("已經刪除第"+i+"郵件。。。。。。。。。"); } folder.close(true); store.close(); System.out.println("delete ok................................."); } catch (NoSuchProviderException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } } /** * reply mail */ public static void replyMail() { //test } } 注意:此實現要求郵箱都支持pop3和smtp協議。現在老的網易郵箱都支持(2006年以前注冊的),所以的sina的 qq的都可以,雅虎的部分支持,具體的可以在網上搜下把。 ============================================================================== 還有一種辦法,也是我以前用到的。 其實最簡單的發郵件方式是用Apache的Common組件中的Email組件,封裝得很不錯。 特簡單。首先從Sun的網站上下載JavaMail框架實現,最新的版本是1.4.1。然後是JavaBeans Activation Framework,最新版本1.1.1,JavaMail需要這個框架。不過如果JDK是1.6的話就不用下了。1.6已經包括了JavaBeans Activation Framework。 最後從 http://commons.apache.org/email/ 下載最新的Common Email,版本1.1。 首先在Eclipse中建立一個新的Java工程,然後把JavaMail和Common Email解壓縮,在工程中添加解壓縮出來的所有Jar的引用。 代碼: package org.fourpane.mail; import org.apache.commons.mail.EmailException; import org.apache.commons.mail.HtmlEmail; public class Mail { public static void main(String[] args) { //SimpleEmail email = new SimpleEmail(); HtmlEmail email = new HtmlEmail(); email.setHostName("smtp.163.com");//郵件伺服器 email.setAuthentication("xingui5624", "******");//smtp認證的用戶名和密碼 try { email.addTo("[email protected]");//收信者 email.setFrom("[email protected]", "******");//發信者 email.setSubject("xingui5624的測試郵件");//標題 email.setCharset("UTF-8");//編碼格式 email.setMsg("這是一封xingui5624的測試郵件");//內容 email.send();//發送 System.out.println("send ok.........."); } catch (EmailException e) { e.printStackTrace(); } } } 【如果發送不成功,可能是你的jar包問題,javamail 的jar可能和jdk1.5以上的j2ee的jar沖突。還有就是可能你的郵箱不支持pop3和smtp協議。】

求採納

『玖』 java中如何實現公司郵箱發送郵件配置

Java中可以通過Javamail API實現公司郵箱郵件發送配置,Java mail是利用現有的郵箱賬戶發送郵件的版工具,具體步權驟如如下:
1、通過JavamailAPI設置發送者郵箱用戶名及密碼
2、通過JavamailAPI設置郵件主題、郵件內容、附件及郵件發送時間
3、通過JavamailAPI設置發送者郵箱地址及接收者郵箱地址,接收者地址可以是多個及抄送
4、郵件的需基本元素都設置完畢後,即可通過Javamail API的發送介面執行發送操作。

閱讀全文

與java郵件發送系統相關的資料

熱點內容
dede工具 瀏覽:507
5g網盟app怎麼下載 瀏覽:486
微信備份老是連接中斷 瀏覽:886
出台多少份文件 瀏覽:380
鞋子怎麼搭配衣服的app 瀏覽:755
文件名使用的通配符的符號是什麼 瀏覽:916
lol分卷文件損壞怎麼辦 瀏覽:276
6分管車螺紋怎麼編程 瀏覽:732
海口農商銀行信用卡app是什麼 瀏覽:770
win10任務欄文件夾我的電腦 瀏覽:14
安卓nba2k18 瀏覽:776
文件夾密碼怎麼修改密碼 瀏覽:271
蘋果數據中心用什麼伺服器 瀏覽:769
省內圓通快遞寄文件夾需要多少錢 瀏覽:740
iphone程序加密 瀏覽:884
win10文件夾調整文件行高 瀏覽:681
創意手繪教程 瀏覽:754
微信刪除帳號信息 瀏覽:596
mysql操作類文件 瀏覽:649
繞過xp密碼 瀏覽:158

友情鏈接