求javamail在线发邮件全部代码,包括身份验证部分,在线,急用 急用 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 刚才正好写了一个163的例子,正好用上.import java.util.Properties;import java.util.Date;import javax.mail.*;import javax.mail.internet.*;import javax.activation.*;public class Test7 { /** * @param args */ public static void main(String[] args) throws Exception { sendhtml(args); } public static void sendhtml(String[] argv) throws Exception { String to = "[email protected]", subject = "null", from = "[email protected]", cc = null, bcc = null; String mailhost = "smtp.163.com"; String username = "XXX"; String password = "XXX"; boolean debug = false; Properties props = System.getProperties(); props.put("mail.smtp.auth", "true"); SMTPAuth auth = new SMTPAuth(username, password); if (mailhost != null) props.put("mail.smtp.host", mailhost); // Get a Session object Session session = Session.getDefaultInstance(props, auth); if (debug) session.setDebug(true); // construct the message Message msg = new MimeMessage(session); if (from != null) msg.setFrom(new InternetAddress(from)); else msg.setFrom(); msg.setRecipients( Message.RecipientType.TO, InternetAddress.parse(to, false)); if (cc != null) msg.setRecipients( Message.RecipientType.CC, InternetAddress.parse(cc, false)); if (bcc != null) msg.setRecipients( Message.RecipientType.BCC, InternetAddress.parse(bcc, false)); subject = new Date().toLocaleString(); msg.setSubject(subject); MimeBodyPart mbp1 = new MimeBodyPart(); String html = "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">" + "<html>" + "<head><title></title></head>" + "<body>" + "<b> see the following jpg : it is a car!</b><br/>" + "<a href=http://www.a.com/a.jsp>hello</a><br/>" + "<IMG src=cid:7e2a34e1.jpg><br/>" + "<b> end of jpg</b>" + "</body>" + "</html>"; mbp1.setContent(html, "text/html"); FileDataSource fds = new FileDataSource("c:/7e2a34e1.jpg"); MimeBodyPart mbp2 = new MimeBodyPart(); mbp2.setFileName(fds.getName()); mbp2.setText("This is a beautiful car !"); mbp2.setDataHandler(new DataHandler(fds)); mbp2.setHeader("Content-ID", "<" + fds.getName() + ">"); mbp2.setDisposition(null); MimeMultipart mp = new MimeMultipart("related"); //alternative mp.addBodyPart(mbp1); mp.addBodyPart(mbp2); msg.setContent(mp); msg.setSentDate(new Date()); Transport.send(msg); System.out.println(mp.getCount()); System.out.println("\nMail was sent successfully."); }}class SMTPAuth extends javax.mail.Authenticator { private String user, password; public SMTPAuth(String u, String p) { user = u; password = p; } public void getuserinfo(String getuser, String getpassword) { user = getuser; password = getpassword; } protected javax.mail.PasswordAuthentication getPasswordAuthentication() { return new javax.mail.PasswordAuthentication(user, password); }} 学习下这方面的知识,还没有用过javaMail呢! 关于jsp传递参数乱码问题 鼠标悬停在一级菜单上,淡出二级菜单怎么实现? 如何读取ORACLE表里BLOB字段的内容? 不显示选择文件窗口直接上传文件的问题 有没有替代request.getRealPath("/");的方法 如何用jsp作一个找回密码的程序? 请问如何将TOMCAT与IIS组合起来使用? 100 高分等答案!! 怎样按时间提取Access里数据?顶有分 如何实现判断一个字符串为null? Extjs+SSH实现消息提示的功能 jsp如何显示word????? 关于一个超级连接的问题。
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;public class Test7 { /**
* @param args
*/
public static void main(String[] args) throws Exception {
sendhtml(args);
} public static void sendhtml(String[] argv) throws Exception { String to = "[email protected]",
subject = "null",
from = "[email protected]",
cc = null,
bcc = null;
String mailhost = "smtp.163.com";
String username = "XXX";
String password = "XXX"; boolean debug = false;
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
SMTPAuth auth = new SMTPAuth(username, password); if (mailhost != null)
props.put("mail.smtp.host", mailhost); // Get a Session object
Session session = Session.getDefaultInstance(props, auth);
if (debug)
session.setDebug(true); // construct the message
Message msg = new MimeMessage(session);
if (from != null)
msg.setFrom(new InternetAddress(from));
else
msg.setFrom(); msg.setRecipients(
Message.RecipientType.TO,
InternetAddress.parse(to, false));
if (cc != null)
msg.setRecipients(
Message.RecipientType.CC,
InternetAddress.parse(cc, false));
if (bcc != null)
msg.setRecipients(
Message.RecipientType.BCC,
InternetAddress.parse(bcc, false)); subject = new Date().toLocaleString();
msg.setSubject(subject); MimeBodyPart mbp1 = new MimeBodyPart();
String html =
"<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">"
+ "<html>"
+ "<head><title></title></head>"
+ "<body>"
+ "<b> see the following jpg : it is a car!</b><br/>"
+ "<a href=http://www.a.com/a.jsp>hello</a><br/>"
+ "<IMG src=cid:7e2a34e1.jpg><br/>"
+ "<b> end of jpg</b>"
+ "</body>"
+ "</html>"; mbp1.setContent(html, "text/html"); FileDataSource fds = new FileDataSource("c:/7e2a34e1.jpg");
MimeBodyPart mbp2 = new MimeBodyPart();
mbp2.setFileName(fds.getName());
mbp2.setText("This is a beautiful car !");
mbp2.setDataHandler(new DataHandler(fds));
mbp2.setHeader("Content-ID", "<" + fds.getName() + ">");
mbp2.setDisposition(null); MimeMultipart mp = new MimeMultipart("related"); //alternative
mp.addBodyPart(mbp1);
mp.addBodyPart(mbp2);
msg.setContent(mp); msg.setSentDate(new Date());
Transport.send(msg);
System.out.println(mp.getCount());
System.out.println("\nMail was sent successfully."); }}
class SMTPAuth extends javax.mail.Authenticator {
private String user, password;
public SMTPAuth(String u, String p) {
user = u;
password = p;
}
public void getuserinfo(String getuser, String getpassword) {
user = getuser;
password = getpassword;
}
protected javax.mail.PasswordAuthentication getPasswordAuthentication() {
return new javax.mail.PasswordAuthentication(user, password);
}
}