在下载使用javax.mail的jar包时候,注意:
有的jar没有包含sun的实现,只包含了api,这类jar名称通常为javax.mail-api-x.x.x.jar,在使用smtp协议发邮件过程中会报错:
- java.lang.ClassNotFoundException: com.sun.mail.util.MailLogger
另一种jar是包含了sun的实现的,名称通常为javax.mail-x.x.x.jar,使用smtp协议发邮件正常!
示例代码:
- package test.mail;
- import javax.mail.Message;
- import javax.mail.Session;
- import javax.mail.Transport;
- import javax.mail.internet.InternetAddress;
- import javax.mail.internet.MimeMessage;
- import java.util.Date;
- import java.util.Properties;
- public class TestMail {
- public static void sendMail(String fromMail, String user, String password,
- String toMail,
- String mailTitle,
- String mailContent) throws Exception {
- Properties props = new Properties(); //可以加载一个配置文件
- // 使用smtp:简单邮件传输协议
- props.put("mail.smtp.host", "smtp.163.com");//存储发送邮件服务器的信息
- props.put("mail.smtp.auth", "true");//同时通过验证
- Session session = Session.getInstance(props);//根据属性新建一个邮件会话
- // session.setDebug(true); //有他会打印一些调试信息。
- MimeMessage message = new MimeMessage(session);//由邮件会话新建一个消息对象
- message.setFrom(new InternetAddress(fromMail));//设置发件人的地址
- message.setRecipient(Message.RecipientType.TO, new InternetAddress(toMail));//设置收件人,并设置其接收类型为TO
- message.setSubject(mailTitle);//设置标题
- //设置信件内容
- // message.setText(mailContent); //发送 纯文本 邮件 todo
- message.setContent(mailContent, "text/html;charset=gbk"); //发送HTML邮件,内容样式比较丰富
- message.setSentDate(new Date());//设置发信时间
- message.saveChanges();//存储邮件信息
- //发送邮件
- // Transport transport = session.getTransport("smtp");
- Transport transport = session.getTransport();
- transport.connect(user, password);
- transport.sendMessage(message, message.getAllRecipients());//发送邮件,其中第二个参数是所有已设好的收件人地址
- transport.close();
- }
- public static void main(String[] args) throws Exception {
- sendMail("用户名@163.com", "用户", "密码",
- "xxxxxxx@qq.com",
- "Java Mail 测试邮件",
- "<a>html 元素</a>:<b>邮件内容</b>");
- }
- }
以上来源:http://blog.csdn.net/conquer0715/article/details/44831851
另:结合以上可以实现一个Email的工具
代码如下:
import java.util.ArrayList; import java.util.Date; import java.util.Iterator; import java.util.List; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Multipart; import javax.mail.PasswordAuthentication; import javax.mail.SendFailedException; 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; import org.apache.log4j.Logger; public class EmailHelper { private final static String default_charset = "UTF-8"; public static enum EncryptionTypes { Default, TLS, SSL; public static EncryptionTypes parseValue(int number){ switch(number){ case 2: return TLS; case 3: return SSL; default: return Default; } } public static EncryptionTypes parseValue(String str){ switch(str){ case "2": return TLS; case "3": return SSL; default: return Default; } } } private static Logger logger = Logger.getLogger(EmailHelper.class); private String mail_host = ""; private int mail_port = 25; private EncryptionTypes encryptionType = EncryptionTypes.Default; private String mail_host_account = ""; private String mail_host_password = ""; private boolean isHtml = false; private boolean isAuth = true; public EmailHelper(String mail_host, int mail_port, EncryptionTypes encryptionType, String account, String password, boolean isHtml) { this.mail_host = mail_host; this.mail_port = mail_port; this.encryptionType = encryptionType; this.mail_host_account = account; this.mail_host_password = password; this.isHtml = isHtml; } public void sendEmail(String senderAddress, String senderName, String receiverAddress, String sub, String msg) throws Exception { String[] address = receiverAddress.split(";"); List<String> recipients = new ArrayList<String>(); for (int i = 0; i < address.length; i++) { if (address[i].trim().length() > 0) { recipients.add(address[i]); } } this.sendEmail(senderAddress, senderName, recipients, sub, msg); } public void sendEmail(String senderAddress, String senderName, List<String> recipients, String sub, String msg) throws SendFailedException { logger.debug("mail subject=" + sub + ", mail_port=" + this.mail_port + ", encryptionType=" + this.encryptionType + ", auth=" + isAuth + ", mail_host_account=" + this.mail_host_account + ", mail_host_password=" + this.mail_host_password); Transport transport = null; try { Properties props = this.getProperties(); Session session = this.getSession(props); MimeMessage message = new MimeMessage(session); if (this.getDefaultIsHtml()) { message.addHeader("Content-type", "text/html"); } else { message.addHeader("Content-type", "text/plain"); } message.setSubject(sub, default_charset); message.setFrom(new InternetAddress(senderAddress, senderName)); for (Iterator<String> it = recipients.iterator(); it.hasNext();) { String email = (String) it.next(); message.addRecipients(Message.RecipientType.TO, email); } Multipart mp = new MimeMultipart(); MimeBodyPart contentPart = new MimeBodyPart(); if (this.getDefaultIsHtml()) { contentPart.setContent( "<meta http-equiv=Content-Type content=text/html; charset=" + default_charset + ">" + msg, "text/html;charset=" + default_charset); } else { contentPart.setText(msg, default_charset); } mp.addBodyPart(contentPart); message.setContent(mp); message.setSentDate(new Date()); if (this.getDefaultEncryptionType() == EncryptionTypes.SSL) { Transport.send(message); } else { transport = session.getTransport("smtp"); transport.connect(this.mail_host, this.mail_port, this.mail_host_account, this.mail_host_password); transport.sendMessage(message, message.getAllRecipients()); } logger.debug("Send email successfully by " + this.getDefaultEncryptionType()); } catch (Exception e) { logger.error("send mail error", e); } finally { if (transport != null) { try { transport.close(); } catch (Exception ex) { } } } } private Properties getProperties() { Properties props = System.getProperties(); EncryptionTypes defaultEncryptionType = this.getDefaultEncryptionType(); //do not need to authenticate smtp account if the user name and password are not set if(this.mail_host_account == null || "".equals(this.mail_host_account) || this.mail_host_password == null || "".equals(this.mail_host_password)){ isAuth = false; props.put("mail.smtp.auth", "false"); } else { props.put("mail.smtp.auth", "true"); } props.put("mail.smtp.host", this.mail_host); if (defaultEncryptionType == EncryptionTypes.TLS) { props.put("mail.smtp.starttls.enable", "true"); } else if (defaultEncryptionType == EncryptionTypes.SSL) { props.put("mail.smtp.socketFactory.port", this.mail_port); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.debug", "true"); props.put("mail.smtp.port", this.mail_port); } return props; } private Session getSession(Properties props) { Session session = null; if (this.getDefaultEncryptionType() == EncryptionTypes.TLS) { session = Session.getInstance(props); } else if (this.getDefaultEncryptionType() == EncryptionTypes.SSL) { session = Session.getInstance(props, new SMTPAuthenticator(this.mail_host_account, this.mail_host_password)); } else { session = Session.getDefaultInstance(props); } return session; } private boolean getDefaultIsHtml() { boolean rst = this.isHtml; return rst; } private class SMTPAuthenticator extends Authenticator { String user; String password; public SMTPAuthenticator(String user, String password) { this.user = user; this.password = password; } protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(this.user, this.password); } } private EncryptionTypes getDefaultEncryptionType() { EncryptionTypes rst = this.encryptionType; if (this.encryptionType == EncryptionTypes.Default) { if (this.mail_port == 465) { rst = EncryptionTypes.SSL; } else if (this.mail_port == 587) { rst = EncryptionTypes.TLS; } } return rst; } }
相关推荐
基于Maven的javax.mail发送邮件例子程序 pom.xml添加依赖jar: <groupId>javax.mail <artifactId>mail ${javamail.version} </dependency> <javamail.version>1.4.1</javamail.version> /* 功能:...
通过java扩展包javax.mail-1.4.4.jar实现邮件发送功能。 import javax.mail.Address; import javax.mail.BodyPart; import javax.mail.Message; import javax.mail.MessagingException; import javax.mail.Multipart...
要使用`javax.mail`发送邮件,你需要配置一个`Properties`对象,设置SMTP服务器信息,然后创建`Session`对象。之后,你可以创建`Message`对象,填充邮件内容,最后通过`Transport`对象发送邮件。例如: ```java ...
javax.mail.jar-Java发送邮件API。Apache Email基于javax.mail做了一个开源项目,参见:http://commons.apache.org/proper/commons-email/
根据提供的文件信息,我们可以深入探讨...以上就是使用`javax.mail`发送邮件的整个流程。通过这种方式,我们可以轻松地集成邮件发送功能到Java应用中,无论是简单的通知邮件还是复杂的批量邮件发送场景都能应对自如。
基于javax.mail的邮件发送测试工具
采用javax.mail发送邮件,可以发送附件
在开发需要发送邮件功能的应用程序时,`javax.mail.jar` 是必不可少的依赖。 标题 "javax.mail.jar下载" 指出,开发者可能遇到了缺少这个库的问题,导致在尝试发送邮件时遇到错误。这通常是因为项目中没有正确地...
`javax.mail-1.6.2_javamailjar1.6.2_javamail-1.6.2_javax.mail_java` 这个标题暗示了我们正在讨论的是 JavaMail 的 1.6.2 版本,这是一个稳定且广泛使用的版本。 JavaMail API 提供了一套标准的接口,用于与邮件...
通过配置会话属性、创建`Message`对象以及使用`Transport`类,可以实现从Java程序发送邮件。同时,它还支持发送HTML内容、附件以及处理复杂的邮件格式。对于需要处理邮件功能的Java项目来说,`javax.mail` 是一个必...
在这个场景中,我们主要关注`javax.mail`库以及如何通过它来发送邮件。 首先,`javax.mail.jar`是Java Mail API的核心库,它提供了邮件处理的各种接口和类。这个库包含了创建、发送、接收和管理电子邮件所需的所有...
Java发送邮件是开发中常见的一项任务,特别是...以上就是Java使用javax.mail.jar和activation.jar发送邮件的基本知识。这两个库的结合使用极大地简化了邮件操作,使得Java开发者能够方便地集成邮件功能到自己的应用中。
总的来说,`javamail1.4.5` 和 `javax.mail-1.5.5.jar` 提供了强大的邮件处理功能,让Java开发者能够轻松地集成邮件功能到他们的应用程序中。选择哪个版本取决于具体项目的需求,包括兼容性、性能和新特性需求。
在这个实例中,我们将利用`javax.mail`和`java.activation`这两个Java库来实现JSP发送邮件的功能。这两个库是JavaMail API的一部分,提供了丰富的接口和类,使得我们可以在Java程序中方便地处理邮件的发送和接收。 ...
1. **Transport API**:这是发送邮件的主要接口,允许开发者通过SMTP(Simple Mail Transfer Protocol)协议将邮件发送到邮件服务器。Transport类提供了发送已构造好的Message对象的方法。 2. **Store API**:该API...
7. **Protocol Support**: `javax.mail` 支持多种邮件协议,如POP3(Post Office Protocol v3)用于接收邮件,SMTP(Simple Mail Transfer Protocol)用于发送邮件,以及IMAP(Internet Message Access Protocol)...
使用javax.mail发送邮件,协议为smtp,依赖版本为1.4.7
Java发送邮件是软件开发中常见的需求,特别是在自动化通知、用户注册...以上就是关于“JAVA发送邮件demo 代码 及jar包 javax.mail.jar”的主要知识点。在实际开发中,可以根据具体需求对这些基础知识进行扩展和调整。
以下是一个简单的Java代码示例,展示了如何使用`javax.mail`发送邮件: ```java import javax.mail.*; import javax.mail.internet.*; public class MailSender { public static void main(String[] args) throws...
在Oracle 11.2.0.4中调用 javax.mail 发送邮件时可能会遇到以下错误: ORA-29532: Java call terminated by uncaught Java exception: javax.mail.MessagingException: IOException while sending message; nested ...