网络选摘之[javamail在线发邮件全部代码,包括身份验证部分]

这篇博客展示了如何使用JavaMail API发送带有HTML内容和图片的电子邮件,包括身份验证步骤。示例代码详细解释了如何设置SMTP服务器、创建MimeMessage、添加MimeBodyPart以及发送邮件的过程。
摘要由CSDN通过智能技术生成

刚才正好写了一个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 = "XX@XX.XX",
subject = "null",
from = "XXX@163.com",
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);
}
}
=====================================================================

看到你是用21CN.com信箱发送的,我就改了一下我的程序.
给你我刚才测试通过的程序.
密码没有加密过.

import java.util.Properties;
import java.util.Date;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class Test7 {

public static void main(String[] args) throws Exception {
sendhtml(args);
}

public static void sendhtml(String[] argv) throws Exception {

String to = "zhouxy@teklink.net",
subject = "null",
from = "XX@21cn.com",
cc = null,
bcc = null;
String mailhost = "smtp.21cn.com";
String username = "XX";
String password = "XX";

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);
}

}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值