『壹』 用javaMail發郵件時出現下面的錯誤是怎麼回事 SMTPSendFailedException: 554 DT:SPM 126 smtp7
554 DT 表示163的伺服器端認為你發出的郵件為垃圾郵件,並不是你的代碼出了問題。如果可以的話,建議用一些不是很大眾化的郵箱地址去實現Java後台發郵件。
『貳』 javamail發郵件始終報javax.mail.NoSuchProviderException:smtp錯誤
可能是版本問題! 如果你用的MyEclipse的話右鍵Build Path->config build path->add library J2EE1.4或者上網下載mail.jar
你的代碼沒問題!或者給你個例子:
package com.mail;
import java.util.Date;
/**
* 郵件類
* @author Dave [email protected]
* 2009-03-26
*/
public class Email{
public final static String TO="[email protected]";
public final static String FROM="[email protected]";
public final static String CC="[email protected]";
public final static String BCC="[email protected]";
public final static String Email_Subject="Test Email With JavaMail";
public final static String Email_Header="This Is Email Header";
public final static String Email_Body="This Is Email Body";
public final static String Email_Host="smtp.xxx.com";
public Date sendDate=null;
public Email(){}
}
package com.mail;
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* 郵件伺服器認證
* @author Dave [email protected]
* 2009-03-26
*/
public class MailAuthenticator extends Authenticator {
private final static String username="xxx";
private final static String password="xxx";
protected PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
}
package com.mail;
import java.io.UnsupportedEncodingException;
import java.util.Date;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Address;
import javax.mail.Authenticator;
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;
/**
* 發送郵件
*
* @author Dave [email protected]
* 2009-03-26
*/
public class SendEmail {
/**
* 發送普通郵件
*
* @throws Exception
*/
public void doSendNormalMail() throws Exception {
Properties prop = new Properties(); // 獲取系統環境
Authenticator auth = new MailAuthenticator(); // 郵件伺服器認證
prop.put("mail.smtp.host", Email.Email_Host);
prop.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(prop, auth);// 設置對話和郵件伺服器進行通訊
Message message = new MimeMessage(session);
/*
* 設置郵件對象
*/
try {
message.setSubject(Email.Email_Subject + "Normal Email"); // 設置郵件主題
message.setContent("Hello", "text/plain"); // 設置郵件格式
message.setText(Email.Email_Body); // 設置郵件體
message.setHeader("Header:", Email.Email_Header); // 設置郵件標題
message.setSentDate(new Date()); // 設置發送時間
Address address = new InternetAddress(Email.FROM, "Dave"); // 設置發信人地址
message.setFrom(address);
/*
* 設置多個發件人地址
*
* Address address[]={new InternetAddress("","") new
* InternetAddress("","")}; message.addFrom(address);
*/
Address toAddress = new InternetAddress(Email.TO); // 設置接收人地址
message.setRecipient(Message.RecipientType.TO, toAddress);
// 設置多個收件人地址
// message.addRecipient(Message.RecipientType.TO,new
// InternetAddress("[email protected]"));
message.saveChanges();
System.out.println("sendNormalEmail() 開始發送郵件……");
Transport.send(message); // 發送郵件
System.out.println("發送成功!");
} catch (MessagingException e) {
System.out.println("發送失敗!");
e.printStackTrace();
}
}
/**
* 發送帶附件的郵件
* @throws UnsupportedEncodingException
*/
public void sendEmailWithAttachment() throws UnsupportedEncodingException {
Properties prop = new Properties();
prop.put("mail.smtp.host", Email.Email_Host);
prop.put("mail.smtp.auth", "true");
Authenticator auth = new MailAuthenticator();
Session session = Session.getDefaultInstance(prop, auth);
Message message = new MimeMessage(session);
try {
message.setSubject(Email.Email_Subject + "Email With Accatchment");// 設置郵件主題
message.setContent("Hello", "text/plain"); // 設置郵件格式
message.setText(Email.Email_Body); // 設置郵件體
message.setHeader("Header:", Email.Email_Header); // 設置郵件標題
message.setSentDate(new Date()); // 設置發送時間
Address address = new InternetAddress(Email.FROM, "Dave"); // 設置發信人地址
message.setFrom(address);
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText("bodypart");
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource("E:\\test.xls");
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName("E:\\test.xls");
multipart.addBodyPart(messageBodyPart);// Put parts in
message.setContent(multipart);
Address toAddress = new InternetAddress(Email.TO);
message.setRecipient(Message.RecipientType.TO, toAddress);
message.saveChanges();
System.out.println("sendEmailWithHtml() 開始發送郵件……");
Transport.send(message);
System.out.println("發送成功!");
} catch (MessagingException e) {
System.out.println("發送失敗!");
e.printStackTrace();
}
}
/**
* 發送帶html的郵件
*
* @throws UnsupportedEncodingException
*/
public void sendEmailWithHtml() throws UnsupportedEncodingException {
Properties prop = new Properties();
Authenticator auth = new MailAuthenticator();
prop.put("mail.smtp.host", Email.Email_Host);
prop.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(prop, auth);
Message message = new MimeMessage(session);
String htmlText = "<h1>Hello</h1>"
+ "<a href=\"http://news.sina.com.cn\" target=\"blank\">點擊連接</a>";
System.out.println(htmlText);
try {
message.setSubject(Email.Email_Subject + "Email With Html");
message.setContent(htmlText + Email.Email_Body,
"text/html;charset=gb2312");
// message.setText(Email.Email_Body);
message.setSentDate(new Date());
Address address = new InternetAddress(Email.FROM, "Dave");
Address toAddress = new InternetAddress(Email.TO);
message.setFrom(address);
message.setRecipient(Message.RecipientType.TO, toAddress);
message.saveChanges();
System.out.println("sendEmailWithHtml() 開始發送郵件……");
Transport.send(message);
System.out.println("發送成功!");
} catch (MessagingException e) {
System.out.println("發送失敗!");
e.printStackTrace();
}
}
/**
* 測試函數
*
* @param args
*/
public static void main(String args[]) {
try {
// 發送普通郵件
new SendEmail().doSendNormalMail();
// 發送帶html的郵件
new SendEmail().sendEmailWithHtml();
// 發送帶附件的郵件
new SendEmail().sendEmailWithAttachment();
} catch (Exception e) {
e.printStackTrace();
}
}
}
『叄』 用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協議。】
求採納