java邮件开发代码 java开发邮件发送功能( 四 )


return attachFileNames;
}
public void setAttachFileNames(String[] attachFileNames) {
this.attachFileNames = attachFileNames;
}
public void setMsAddress(String[] msAddress) {
this.msAddress = msAddress;
}
public String[] getMsAddress() {
return msAddress;
}
public void setDebug(boolean debug) {
this.debug = debug;
}
public boolean isDebug() {
return debug;
}
public void setTimeout(long timeout) {
this.timeout = timeout;
}
public long getTimeout() {
return timeout;
}
}
public class MyAuthenticator extends Authenticator {
private String username = null;
private 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);
}
}
注意一下:
Myeclipse自带的JavaEE5.jar和java mail会发生冲突
找到ME下的javeee包
D:\MyEclipse 8.5\Common\plugins\com.genuitec.eclipse.j2eedt.core_8.5.0.me201003231033\data\libraryset\EE_5\javaee.jar
用rar等解压工具解开javaee.jar,删除里面的javax\mail文件夹(可以先备份javaee.jar)
也即,以后都不能使用javaee.jar里面的邮件api发送邮件了.
用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 = "xingui5624@sina.com";//发件人地址String to = "ilovenumen@vip.sina.com";//接受地址(必须支持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; imsg.length; i++){Message message = msg[i];Address address[] = message.getFrom();StringBuffer from = new StringBuffer();/*** 此for循环是我项目测试用的*/for(int j=0; jaddress.length; j++){if (j0)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);//设置已删除状态为trueif(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 。最后从下载最新的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("xingui5624@163.com");//收信者email.setFrom("xingui5624@126.com", "******");//发信者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协议 。】

推荐阅读