javaMail简单的发送邮件例子 在线急等 我实验成功就揭贴 100分 代码越简单越好 只要我能实验成功就马上揭贴 在线急等 大量分等你来拿呀 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 package com.lhkj.util;public class Email { private String mailserver;// 发出邮箱的服务器 private String from;// 发出的邮箱 private String to;// 发到的邮箱 private String subject;// 标题 private String type;// 发送邮件格式 private String messageText;// 发送的内容 private String namse;// 保存用户名 private String password;// 保存密码 public String getMailserver() { return mailserver; } public void setMailserver(String mailserver) { this.mailserver = mailserver; } public String getTo() { return to; } public void setTo(String to) { this.to = to; } public String getType() { return type; } public void setType(String type) { this.type = type; } public String getMessageText() { return messageText; } public void setMessageText(String messageText) { this.messageText = messageText; } public String getFrom() { return from; } public void setFrom(String from) { this.from = from; } public String getSubject() { return subject; } public void setSubject(String subject) { this.subject = subject; } public String getNamse() { return namse; } public void setNamse(String namse) { this.namse = namse; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; }}我的实体类 package com.mycom.javamaildemo.mail;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 com.mycom.javamaildemo.vo.MyAuthenticator;public class Test{ public static void main(String[] args) { String host = "smtp.163.com"; //发件人使用发邮件的电子信箱服务器 String from = "[email protected]"; //发邮件的出发地(发件人的信箱) String to = "[email protected]"; //发邮件的目的地(收件人信箱) // Get system properties Properties props = System.getProperties(); // Setup mail server props.put("mail.smtp.host", host); // Get session props.put("mail.smtp.auth", "true"); //这样才能通过验证 MyAuthenticator myauth = new MyAuthenticator("[email protected]", "这里写的是我邮箱的密码"); Session session = Session.getDefaultInstance(props, myauth);// session.setDebug(true); // Define message MimeMessage message = new MimeMessage(session); try { //Set the from address message.setFrom(new InternetAddress(from)); message.addRecipient(Message.RecipientType.TO, new InternetAddress(to)); // Set the subject message.setSubject("测试程序!"); // Set the content message.setText("这是用java写的发送电子邮件的测试程序!"); message.saveChanges();//56行 Transport.send(message); } catch (AddressException e) { e.printStackTrace(); } catch (MessagingException e) { e.printStackTrace(); } }} //类2package com.mycom.javamaildemo.vo;import javax.mail.PasswordAuthentication;public class MyAuthenticator extends javax.mail.Authenticator { private String strUser; private String strPwd; public MyAuthenticator(String user, String password) { this.strUser = user; this.strPwd = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(strUser, strPwd); } }必须支持smtp协议,126邮箱和163邮箱是不支持的,不要用,好像sohu的可以! import java.util.Properties; import javax.activation.DataHandler; import javax.activation.FileDataSource; import javax.mail.Message; 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; public class HtmlMessageSender { String protocol = "smtp"; String from = "[email protected]"; String to = "[email protected] "; String subject = "HTML测试"; String body = "<a href=http://www.it315.org>" + "欢迎大家访问我们的网站</a></br>" + "<img src=\"cid:it315_logo_gif\">"; public static void main(String[] args) throws Exception { String server = "smtp.sina.com.cn"; String user = "it315_test"; String pass = "123456"; HtmlMessageSender sender = new HtmlMessageSender (); Session session = sender.createSession(); MimeMessage message = sender.createMessage(session); //获得Transport对象,并连接邮件服务器发送邮件 Transport transport = session.getTransport(); transport.connect(server, user, pass); transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO)); transport.close(); } public Session createSession() { Properties props = new Properties(); props.setProperty("mail.transport.protocol", protocol); /*必须将mail.smtp.auth属性设置为true,SMTPTransport对象才会向 SMTP服务器提交用户认证信息,这个信息可以从JavaMail的javadocs文档 中的com.sun.mail.smtp包的帮助页面内查看到。 */ props.setProperty ("mail.smtp.auth","true"); Session session = Session.getInstance(props); session.setDebug(true); return session; } public MimeMessage createMessage(Session session) throws Exception { MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress(from)); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to)); message.setSubject(subject); MimeMultipart multipart = new MimeMultipart("related"); MimeBodyPart htmlBodyPart = new MimeBodyPart(); htmlBodyPart.setContent(body,"text/html;charset=gb2312"); multipart.addBodyPart(htmlBodyPart); MimeBodyPart gifBodyPart = new MimeBodyPart(); FileDataSource fds = new FileDataSource("d:\\attach\\logo.gif"); gifBodyPart.setDataHandler(new DataHandler(fds)); gifBodyPart.setContentID("it315_logo_gif"); multipart.addBodyPart(gifBodyPart); message.setContent(multipart); message.saveChanges(); return message; } } sql问题求解答 java简历 跪求高手-----请教下谁知道fckeditor的一键排版功能 .java 中调用 .jar 文件 !!! 请问webwork2中如何得到页面文本框提交的日期? 如果不用listener而用servlet,如何每隔1分钟启动一个程序呢 关于tomcat5部署servlet的问题 我写的一个socket编程提交表单不成功,那位老大给指点一下 Socket客户机/服务器问题,急! WAS的JTA事务配置 SSH框架时间问题 腾讯网的FLASH超链接屏蔽如何实现?
private String mailserver;// 发出邮箱的服务器
private String from;// 发出的邮箱
private String to;// 发到的邮箱
private String subject;// 标题
private String type;// 发送邮件格式
private String messageText;// 发送的内容
private String namse;// 保存用户名
private String password;// 保存密码 public String getMailserver() {
return mailserver;
} public void setMailserver(String mailserver) {
this.mailserver = mailserver;
} public String getTo() {
return to;
} public void setTo(String to) {
this.to = to;
} public String getType() {
return type;
} public void setType(String type) {
this.type = type;
} public String getMessageText() {
return messageText;
} public void setMessageText(String messageText) {
this.messageText = messageText;
} public String getFrom() {
return from;
} public void setFrom(String from) {
this.from = from;
} public String getSubject() {
return subject;
} public void setSubject(String subject) {
this.subject = subject;
} public String getNamse() {
return namse;
} public void setNamse(String namse) {
this.namse = namse;
} public String getPassword() {
return password;
} public void setPassword(String password) {
this.password = password;
}}
我的实体类
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 com.mycom.javamaildemo.vo.MyAuthenticator;public class Test
{
public static void main(String[] args)
{
String host = "smtp.163.com"; //发件人使用发邮件的电子信箱服务器
String from = "[email protected]"; //发邮件的出发地(发件人的信箱)
String to = "[email protected]"; //发邮件的目的地(收件人信箱) // Get system properties
Properties props = System.getProperties(); // Setup mail server
props.put("mail.smtp.host", host); // Get session
props.put("mail.smtp.auth", "true"); //这样才能通过验证 MyAuthenticator myauth = new MyAuthenticator("[email protected]", "这里写的是我邮箱的密码");
Session session = Session.getDefaultInstance(props, myauth);// session.setDebug(true); // Define message
MimeMessage message = new MimeMessage(session);
try
{
//Set the from address
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
// Set the subject
message.setSubject("测试程序!");
// Set the content
message.setText("这是用java写的发送电子邮件的测试程序!");
message.saveChanges();//56行
Transport.send(message);
}
catch (AddressException e)
{
e.printStackTrace();
}
catch (MessagingException e)
{
e.printStackTrace();
}
}} //类2package com.mycom.javamaildemo.vo;import javax.mail.PasswordAuthentication;public class MyAuthenticator extends javax.mail.Authenticator {
private String strUser;
private String strPwd;
public MyAuthenticator(String user, String password)
{
this.strUser = user;
this.strPwd = password;
} protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication(strUser, strPwd);
}
}必须支持smtp协议,126邮箱和163邮箱是不支持的,不要用,好像sohu的可以!
import javax.activation.DataHandler;
import javax.activation.FileDataSource;
import javax.mail.Message;
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;
public class HtmlMessageSender
{
String protocol = "smtp";
String from = "[email protected]";
String to = "[email protected] ";
String subject = "HTML测试";
String body = "<a href=http://www.it315.org>" +
"欢迎大家访问我们的网站</a></br>" +
"<img src=\"cid:it315_logo_gif\">"; public static void main(String[] args) throws Exception
{
String server = "smtp.sina.com.cn";
String user = "it315_test";
String pass = "123456"; HtmlMessageSender sender = new HtmlMessageSender ();
Session session = sender.createSession();
MimeMessage message = sender.createMessage(session); //获得Transport对象,并连接邮件服务器发送邮件
Transport transport = session.getTransport();
transport.connect(server, user, pass);
transport.sendMessage(message,
message.getRecipients(Message.RecipientType.TO));
transport.close();
} public Session createSession()
{
Properties props = new Properties();
props.setProperty("mail.transport.protocol", protocol);
/*必须将mail.smtp.auth属性设置为true,SMTPTransport对象才会向
SMTP服务器提交用户认证信息,这个信息可以从JavaMail的javadocs文档
中的com.sun.mail.smtp包的帮助页面内查看到。
*/
props.setProperty ("mail.smtp.auth","true");
Session session = Session.getInstance(props);
session.setDebug(true);
return session;
} public MimeMessage createMessage(Session session) throws Exception
{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(subject); MimeMultipart multipart = new MimeMultipart("related"); MimeBodyPart htmlBodyPart = new MimeBodyPart();
htmlBodyPart.setContent(body,"text/html;charset=gb2312");
multipart.addBodyPart(htmlBodyPart); MimeBodyPart gifBodyPart = new MimeBodyPart();
FileDataSource fds = new FileDataSource("d:\\attach\\logo.gif");
gifBodyPart.setDataHandler(new DataHandler(fds));
gifBodyPart.setContentID("it315_logo_gif");
multipart.addBodyPart(gifBodyPart); message.setContent(multipart);
message.saveChanges();
return message;
}
}