【发送带有HTML内容的电子邮件】在发送电子邮件时, 我们也可以发送html内容。
为了更好地理解此示例, 请首先学习使用JavaMail API发送电子邮件的步骤。 |
为了使用JavaMail API接收或发送电子邮件, 你需要加载两个jar文件:mail.jar activation.jar下载这些jar文件(或)到Oracle网站下载最新版本。 |
使用JavaMail API发送带有html内容的电子邮件的示例
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
class SendHtmlEmail
{
public static void main(String [] args)
{String host="mail.srcmini.com";
//change accordingly
String to="sonoojaiswal1987@gmail.com";
//change accordingly
final String user="sonoojaiswal@srcmini.com";
//change accordingly
final String password="xxxxx";
//change accordinglyProperties properties = System.getProperties();
properties.setProperty("mail.smtp.host", );
properties.put("mail.smtp.auth", "true");
Session session = Session.getDefaultInstance(properties, new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(user, password);
}
});
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(user));
message.addRecipient(Message.RecipientType.TO, new InternetAddress(to));
message.setSubject("HTML Message");
message.setContent("<
h1>
sending html mail check<
/h1>
", "text/html" );
Transport.send(message);
System.out.println("message sent....");
}catch (MessagingException ex) {ex.printStackTrace();
}
}
}
加载jar文件 |
c:\>
set classpath = mail.jar;
activation.jar;
。;
|
编译源文件 |
c:\>
javac SendHtmlEmail.java |
run by |
c:\>
Java SendHtmlEmail |
推荐阅读