导航:首页 > 编程语言 > javamail126

javamail126

发布时间:2023-11-25 19:57:39

『壹』 用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协议。】

求采纳

阅读全文

与javamail126相关的资料

热点内容
中间夹菜单里面不能显示压缩文件 浏览:952
如何指导小学生参加编程比赛 浏览:275
物业的招标文件有哪些 浏览:452
保存游戏文件名非法或只读 浏览:258
js怎么做图片时钟 浏览:451
华为应用里面有了app说明什么 浏览:801
数据库中xy是什么意思 浏览:893
u盘打不开提示找不到应用程序 浏览:609
网站功能介绍怎么写 浏览:954
word在试图打开文件时错误 浏览:108
主板无vga插槽怎么连接编程器 浏览:521
录视频文件在哪里删除 浏览:881
word2013如何插入文件 浏览:233
proe教程百度网盘 浏览:197
如何控制远程linux服务器 浏览:740
it教学app有哪些 浏览:34
怎么在ps抠的图变成矢量文件 浏览:405
口袋妖怪银魂安卓v11 浏览:1
网站上芒果tv的账号都是什么 浏览:104
带公式的表格如何刷新数据 浏览:81

友情链接