Java简单的本地邮箱服务器发送(javax.mail)

推荐使用标题5

1.安装使用易游服务器以及Foxmail

易游服务器傻瓜式安装
Foxmail 向导部分请断网

2.简单的入门案例

  • 1.创建mevan项目
  • 2.引入坐标
<!-- Javamail -->
    <dependency>
        <groupId>javax.mail</groupId>
        <artifactId>mail</artifactId>
        <version>1.4.4</version>
    </dependency>
  • 3 入门邮箱代码
/*
    java 邮箱的入门案例
 */
public class MailDemo1 {
    public static void main(String[] args) throws Exception{
        //1.配置发送邮箱的属性信息
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.host","localhost");// 设置stmp协议主机(案例:使用我们本机/实际:取邮箱POP3/SMTP服务查找)
        properties.setProperty("mail.smtp.auth","true");//设置stmp是否需要认证
        //2.使用属性打开一个mail的会话 -->这里的session使用的是javax.mail.Session;
        Session session = Session.getInstance(properties);
        //3.设置会话为debug模式  ---> 可以不设置 设置后操作打印会更精细
        session.setDebug(true);
        //4.创建邮件的主体信息对象
        MimeMessage mimeMessage = new MimeMessage(session);
        //5.写入邮件内容
        mimeMessage.setFrom(new InternetAddress("sass_ee88_01@export.com"));//设置发件人
        mimeMessage.setSubject("测试邮件");//设置邮件主题
        mimeMessage.setText("第一封java发送的邮件");//设置邮件的返送文本内容
        /** TO : 发送   正常 一对一 发送  能看到收件人
         *  CC : 抄送   一对多  很多人都能收到  能看到收件人
         *  BCC : 密送    看不到收件人
         *  Message.RecipientType.TO --> .BCC  ---> .CC
         */
        mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress("sass_ee88_02@export.com")); //设置收件人
        //6.获取发送器对象
        Transport transport = session.getTransport("smtp");//提供使用协议
        //7.设置发送人信息(补充发件人信息)
        transport.connect("localhost","sass_ee88_01","1234");
        //8.发送邮箱    填入发送的内容  收件人对象(此参数为所有的收件人)
        transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
        //9.释放资源
        transport.close();
    }
}

3.简单邮件代码提取成为工具类

package com.itheima.util;

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.Map;
import java.util.Properties;

/**
 * 抽取工具类
 * @author 黑马程序员
 * @Company http://www.itheima.com
 */
public class MailUtil {

    /**
     *
     * @param map           收件人地址   key是邮件地址  value是邮件的类型
     * @param subject       邮件主题
     * @param content       邮件内容
     * @throws Exception
     */
    public static void sendMail(Map<String,String> map , String subject, String content)throws Exception{
        //1.配置发送邮件的属性信息
        Properties properties = new Properties();
        properties.setProperty("mail.smtp.host","localhost");//设置stmp协议的主机
        properties.setProperty("mail.smtp.auth","true");//设置smtp是否需要认证
        //2.使用属性打开一个mail的会话
        Session session = Session.getInstance(properties);
        //3.设置会话是debug模式
        session.setDebug(true);
        //4.创建邮件的主体信息对象
        MimeMessage mimeMessage = new MimeMessage(session);
        //5.设置发件人
        mimeMessage.setFrom(new InternetAddress("saas_server@export.com"));
        //6.设置邮件主题
        mimeMessage.setSubject(subject);
        //7.设置邮件正文
        mimeMessage.setText(content);
        //8.设置收件人
        for(Map.Entry<String,String> me : map.entrySet()){
            String email = me.getKey();//邮件地址
            String type = me.getValue();//邮件类型
            if("to".equalsIgnoreCase(type)){
                //发送
                mimeMessage.setRecipient(Message.RecipientType.TO,new InternetAddress(email));//TO:发送  CC:抄送   BCC:密送
            }else if("cc".equalsIgnoreCase(type)){
                //CC:抄送
                mimeMessage.setRecipient(Message.RecipientType.CC,new InternetAddress(email));
            }else if("bcc".equalsIgnoreCase(type)){
                //BCC:密送
                mimeMessage.setRecipient(Message.RecipientType.BCC,new InternetAddress(email));
            }
        }

        //9.获取发送器对象
        Transport transport = session.getTransport("smtp");//提供使用的协议
        //10.设置发件人的信息
        transport.connect("localhost","saas_server","1234");
        //11.发送邮件
        transport.sendMessage(mimeMessage,mimeMessage.getAllRecipients());
        //12.释放资源
        transport.close();
    }
}

利用工具类 发送邮件

 public static void main(String[] args) throws Exception{
        Map<String,String> map = new HashMap<>();
        map.put("saas_ee88@export.com","to");
        map.put("saas_ee88_01@export.com","cc");
        map.put("saas_ee88_02@export.com","bcc");
        MailUtil.sendMail(map,"测试第二封邮件","使用工具类发送邮件....");
    }

4 javax.mail与spring整合

导入俩者坐标

    <!-- Javamail -->
        <dependency>
            <groupId>javax.mail</groupId>
            <artifactId>mail</artifactId>
            <version>1.4.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework</groupId>
            <artifactId>spring-context-support</artifactId>
            <version>5.0.4.RELEASE</version>
        </dependency>

mail.properties配置文件

#发件箱名称
mail.from=sass_ee88_01@export.com
#指定smtp服务器地址
mail.host=localhost
#指定发件箱的登陆用户名
mail.username=sass_ee88_01
#指定发件箱的登陆密码
mail.password=1234
#指定发送邮箱所用的编码
mail.encoding=UTF-8
#指定发送邮件的所用的协议 不写默认也是smtp
mail.protocol=smtp

applicationContext-mail.xml配置文件

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xsi:schemaLocation="http://www.springframework.org/schema/beans
       					   http://www.springframework.org/schema/beans/spring-beans.xsd
       					   http://www.springframework.org/schema/context
       					   http://www.springframework.org/schema/context/spring-context.xsd">
    <!-- 指定引入其他资源文件.properties文件 -->
    <context:property-placeholder location="classpath:mail.properties"/>

    <!-- 配置简单邮件消息对象 -->
    <bean id="mailMessage" class="org.springframework.mail.SimpleMailMessage">
        <!-- 此时我们只需要注入发件箱名称即可。不要注入主题,正文,收件箱等等信息,因为那些信息是不固定的 -->
        <property name="from" value="${mail.from}"></property>
    </bean>

    <!-- 配置邮件发送器 -->
    <bean id="mailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
        <property name="host" value="${mail.host}"></property>
        <property name="username" value="${mail.username}"></property>
        <property name="password" value="${mail.password}"></property>
        <property name="defaultEncoding" value="${mail.encoding}"></property>
        <property name="protocol" value="${mail.protocol}"></property>
        <property name="javaMailProperties">
            <props>
                <prop key="mail.smtp.auth">true</prop><!-- 是否需要验证 -->
                <prop key="mail.debug">true</prop><!-- 是否需要debug的信息 -->
                <prop key="mail.smtp.timeout">0</prop><!-- 设置发送超时时间,以秒为单位。0为永不超时 -->
            </props>
        </property>
    </bean>
</beans>

java运行测试代码

import org.springframework.context.ApplicationContext;
import org.springframework.context.support.ClassPathXmlApplicationContext;
import org.springframework.mail.SimpleMailMessage;
import org.springframework.mail.javamail.JavaMailSenderImpl;

/**
 *  javaMail 和 spring整合的邮件发送
 */
public class MailDemo03 {
    public static void main(String[] args) {
        //获取spring容器对象
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-mail.xml");
        // 获取消息对象 通过spring提供的
        SimpleMailMessage mailMessage = (SimpleMailMessage)context.getBean("mailMessage");
        // 准备邮件内容
        mailMessage.setSubject("spring和JavaMail整合的邮件发送第一次测试");//设置邮件主题
        mailMessage.setText("这是一封spring和JavaMail整合的邮件发送第一次测试"); //设置邮件文本内容
        mailMessage.setTo("sass_ee88_02@export.com");//设置收件人信息  ,此处可以多写 , 隔开
//        mailMessage.setCc("sass_ee88_02@export.com");
//        mailMessage.setBcc("sass_ee88_02@export.com");
        // 获取发送器对象 通过spring提供的
        JavaMailSenderImpl sender = (JavaMailSenderImpl) context.getBean("mailSender");
        //last end  发送邮箱
        sender.send(mailMessage);
    }
}

5 上面的只是简单的邮件发送 (正常使用还有图片以及附件的发送)

**这里我们就可以使用spring提供的复杂邮件帮助对象了**
/**
 * 带附件以及图片的邮件发送
 */
public class MailDeo04 {
    public static void main(String[] args)throws Exception {
        //1.获取spring容器
        ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext-mail.xml");
        //2.获取发送器对象
        JavaMailSenderImpl sender = (JavaMailSenderImpl) context.getBean("mailSender");
        //3.使用获取MimeMessage(多用途物联网邮件数据类型)
        MimeMessage mimeMessage = sender.createMimeMessage();
        //4.创建spring提供的复杂邮件的帮助对象
        MimeMessageHelper helper = new MimeMessageHelper(mimeMessage,true);//是否为多部分数据
        //5.写入邮件信息
        helper.setFrom("sass_ee88_01@export.com");//设置发件人邮箱
        helper.setTo("sass_ee88_02@export.com");//设置收件人邮箱
        helper.setSubject("我是一封多数据格式的邮件");//设置邮件的主题
        String data  = "<html><head></head><body><h1>你好</h1>  <img src=cid:image ></body></html>" ;
        helper.setText(data,true);//设置邮件的正文   是否为html文本内容
        //填充 设置 image 图片路径
        FileSystemResource resource = new FileSystemResource(new File("E:\\img\\13.jpg"));
        //使用流  图片替换 cid 中image图片
        helper.addInline("image",resource);
        //添加附件  附件名称  附件来源流
        helper.addAttachment("1.jpg",resource);
        // last end  发送邮件
        sender.send(mimeMessage);

    }
  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
用于Java的邮件发送的一个类方法 Java邮件发送类 这是第一部分第一个类:MailSenderInfo.java 1. package com.util.mail; 2. /** 3. * 发送邮件需要使用的基本信息 4. */ 5. import java.util.Properties; 6. public class MailSenderInfo { 7. // 发送邮件的服务器的IP和端口 8. private String mailServerHost; 9. private String mailServerPort = "25"; 10. // 邮件发送者的地址 11. private String fromAddress; 12. // 邮件接收者的地址 13. private String toAddress; 14. // 登陆邮件发送服务器的用户名和密码 15. private String userName; 16. private String password; 17. // 是否需要身份验证 18. private boolean validate = false; 19. // 邮件主题 20. private String subject; 21. // 邮件的文本内容 22. private String content; 23. // 邮件附件的文件名 24. private String[] attachFileNames; 25. /** 26. * 获得邮件会话属性 27. */ 28. public Properties getProperties(){ 29. Properties p = new Properties(); 30. p.put("mail.smtp.host", this.mailServerHost); 31. p.put("mail.smtp.port", this.mailServerPort); 32. p.put("mail.smtp.auth", validate ? "true" : "false"); 33. return p; 34. } 35. public String getMailServerHost() { 36. return mailServerHost; 37. } 38. public void setMailServerHost(String mailServerHost) { 39. this.mailServerHost = mailServerHost; 40. } 41. public String getMailServerPort() { 42. return mailServerPort; 43. } 44. public void setMailServerPort(String mailServerPort) { 45. this.mailServerPort = mailServerPort; 46. } 47. public boolean isValidate() { 48. return validate; 49. } 50. public void setValidate(boolean validate) { 51. this.validate = validate; 52. } 53. public String[] getAttachFileNames() { 54. return attachFileNames; 55. } 56. public void setAttachFileNames(String[] fileNames) { 57. this.attachFileNames = fileNames; 58. } 59. public String getFromAddress() { 60. return fromAddress; 61. } 62. public void setFromAddress(String fromAddress) { 63. this.fromAddress = fromAddress; 64. } 65. public String getPassword() { 66. return password; 67. } 68. public void setPassword(String password) { 69. this.password = password; 70. } 71. public String getToAddress() { 72. return toAddress; 73. } 74. public void setToAddress(String toAddress) { 75. this.toAddress = toAddress; 76. } 77. public String getUserName() { 78. return userName; 79. } 80. public void setUserName(String userName) { 81. this.userName = userName; 82. } 83. public String getSubject() { 84. return subject; 85. } 86. public void setSubject(String subject) { 87. this.subject = subject; 88. } 89. public String getContent() { 90. return content; 91. } 92. public void setContent(String textContent) { 93. this.content = textContent; 94. } 95. } 第二部分第二个类:SimpleMailSender.java Java代码 1. package com.util.mail; 2. 3. import java.util.Date; 4. import java.util.Properties; 5. import javax.mail.Address; 6. import javax.mail.BodyPart; 7. import javax.mail.Message; 8. import javax.mail.MessagingException; 9. import javax.mail.Multipart; 10. import javax.mail.Session; 11. import javax.mail.Transport; 12. import javax.mail.internet.InternetAddress; 13. import javax.mail.internet.MimeBodyPart; 14. import javax.mail.internet.MimeMessage; 15. import javax.mail.internet.MimeMultipart; 16. 17. /** 18. * 简单邮件(不带附件的邮件)发送器 19. */ 20. public class SimpleMailSender { 21. /** 22. * 以文本格式发送邮件 23. * @param mailInfo 待发送的邮件的信息 24. */ 25. public boolean sendTextMail(MailSenderInfo mailInfo) { 26. // 判断是否需要身份认证 27. MyAuthenticator authenticator = null; 28. Properties pro = mailInfo.getProperties(); 29. if (mailInfo.isValidate()) { 30. // 如果需要身份认证,则创建一个密码验证器 31. authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); 32. } 33. // 根据邮件会话属性和密码验证器构造一个发送邮件的session 34. Session sendMailSession = Session.getDefaultInstance(pro,authenticator); 35. try { 36. // 根据session创建一个邮件消息 37. Message mailMessage = new MimeMessage(sendMailSession); 38. // 创建邮件发送者地址 39. Address from = new InternetAddress(mailInfo.getFromAddress()); 40. // 设置邮件消息的发送者 41. mailMessage.setFrom(from); 42. // 创建邮件的接收者地址,并设置到邮件消息中 43. Address to = new InternetAddress(mailInfo.getToAddress()); 44. mailMessage.setRecipient(Message.RecipientType.TO,to); 45. // 设置邮件消息的主题 46. mailMessage.setSubject(mailInfo.getSubject()); 47. // 设置邮件消息发送的时间 48. mailMessage.setSentDate(new Date()); 49. // 设置邮件消息的主要内容 50. String mailContent = mailInfo.getContent(); 51. mailMessage.setText(mailContent); 52. // 发送邮件 53. Transport.send(mailMessage); 54. return true; 55. } catch (MessagingException ex) { 56. ex.printStackTrace(); 57. } 58. return false; 59. } 60. 61. /** 62. * 以HTML格式发送邮件 63. * @param mailInfo 待发送的邮件信息 64. */ 65. public static boolean sendHtmlMail(MailSenderInfo mailInfo){ 66. // 判断是否需要身份认证 67. MyAuthenticator authenticator = null; 68. Properties pro = mailInfo.getProperties(); 69. //如果需要身份认证,则创建一个密码验证器 70. if (mailInfo.isValidate()) { 71. authenticator = new MyAuthenticator(mailInfo.getUserName(), mailInfo.getPassword()); 72. } 73. // 根据邮件会话属性和密码验证器构造一个发送邮件的session 74. Session sendMailSession = Session.getDefaultInstance(pro,authenticator); 75. try { 76. // 根据session创建一个邮件消息 77. Message mailMessage = new MimeMessage(sendMailSession); 78. // 创建邮件发送者地址 79. Address from = new InternetAddress(mailInfo.getFromAddress()); 80. // 设置邮件消息的发送者 81. mailMessage.setFrom(from); 82. // 创建邮件的接收者地址,并设置到邮件消息中 83. Address to = new InternetAddress(mailInfo.getToAddress()); 84. // Message.RecipientType.TO属性表示接收者的类型为TO 85. mailMessage.setRecipient(Message.RecipientType.TO,to); 86. // 设置邮件消息的主题 87. mailMessage.setSubject(mailInfo.getSubject()); 88. // 设置邮件消息发送的时间 89. mailMessage.setSentDate(new Date()); 90. // MiniMultipart类是一个容器类,包含MimeBodyPart类型的对象 91. Multipart mainPart = new MimeMultipart(); 92. // 创建一个包含HTML内容的MimeBodyPart 93. BodyPart html = new MimeBodyPart(); 94. // 设置HTML内容 95. html.setContent(mailInfo.getContent(), "text/html; charset=utf-8"); 96. mainPart.addBodyPart(html); 97. // 将MiniMultipart对象设置为邮件内容 98. mailMessage.setContent(mainPart); 99. // 发送邮件 100. Transport.send(mailMessage); 101. return true; 102. } catch (MessagingException ex) { 103. ex.printStackTrace(); 104. } 105. return false; 106. } 107. } 第三个类:MyAuthenticator.java Java代码 1. package com.util.mail; 2. 3. import javax.mail.*; 4. 5. public class MyAuthenticator extends Authenticator{ 6. String userName=null; 7. String password=null; 8. 9. public MyAuthenticator(){ 10. } 11. public MyAuthenticator(String username, String password) { 12. this.userName = username; 13. this.password = password; 14. } 15. protected PasswordAuthentication getPasswordAuthentication(){ 16. return new PasswordAuthentication(userName, password); 17. } 18. } 19. 下面给出使用上面三个类的代码: Java代码 1. public static void main(String[] args){ 2. //这个类主要是设置邮件 3. MailSenderInfo mailInfo = new MailSenderInfo(); 4. mailInfo.setMailServerHost("smtp.163.com"); 5. mailInfo.setMailServerPort("25"); 6. mailInfo.setValidate(true); 7. mailInfo.setUserName("[email protected]"); 8. mailInfo.setPassword("**********");//您的邮箱密码 9. mailInfo.setFromAddress("[email protected]"); 10. mailInfo.setToAddress("[email protected]"); 11. mailInfo.setSubject("设置邮箱标题"); 12. mailInfo.setContent("设置邮箱内容"); 13. //这个类主要来发送邮件 14. SimpleMailSender sms = new SimpleMailSender(); 15. sms.sendTextMail(mailInfo);//发送文体格式 16. sms.sendHtmlMail(mailInfo);//发送html格式 17. }
package com.email.send; 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 EmailSender { String protocol ="smtp"; //服务器 String from="*******@qq.com";//寄信人,这里可以改为你的邮箱地址 String to="*******@qq.com";//收信人,改为你要寄给那人的邮箱地址 建议可以用自己的QQ邮箱试试 String subject="Java发送邮件测试!";//邮件主题 String body="<a href=http://www.qq.com>qq.com" + "</a><br><img src=\"cid:8.jpg\">";//邮件内容 这是一个HTTP网页类的邮件内容 public static void main(String []args)throws Exception{ String server ="smtp.qq.com";//QQ邮箱的服务器,例如新浪邮箱或者其他服务器可以自己去查,这里用QQ邮箱为例 String user="******@qq.com";//登录邮箱的用户,不如说你用QQ邮箱,那就写你登录QQ邮箱的帐号 String pass="*******";//登录密码 EmailSender sender=new EmailSender(); Session session= sender.createSession(); MimeMessage message=sender.createMessage(session); System.out.println("正在发送邮件..."); Transport transport=session.getTransport(); transport.connect(server,user,pass); transport.sendMessage(message,message.getRecipients(Message.RecipientType.TO)); transport.close(); System.out.println("发送成功!!!"); } public Session createSession(){ Properties props=new Properties(); props.setProperty("mail.transport.protocol", protocol); 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 bodyPart=new MimeBodyPart(); bodyPart.setContent(body,"text/html;charset=gb2312"); multipart.addBodyPart(bodyPart); //发附件 MimeBodyPart gifBodyPart=new MimeBodyPart(); FileDataSource fds=new FileDataSource("f:\\音乐.MP3");//F盘下的 音乐.MP3 文件。这里看个人情况 gifBodyPart.setDataHandler(new DataHandler(fds)); gifBodyPart.setContentID("音乐.MP3"); multipart.addBodyPart(gifBodyPart); message.setContent(multipart); message.saveChanges(); return message; } } //PS:把java发送邮件的导入的JAR包上传了,解压后导入你的项目里就可
package com.hx.mail; import java.util.HashMap; import java.util.Map; import java.util.Properties; import javax.mail.Authenticator; import javax.mail.Message; import javax.mail.Session; import javax.mail.PasswordAuthentication; import javax.mail.Transport; import javax.mail.internet.InternetAddress; import javax.mail.internet.MimeMessage; /** * MailServices 邮件接收发送接口实现类 * * @author [email protected] * Date 2010-05-11 * @version 1.0 */ public class KumMailServicesImpl implements KumMailServices { /** MailboxType 邮箱类型 */ private static Map<String,String> MailboxTypes = null; /** host 邮箱服务器类型 */ private String host = null; /** sender 邮件发送者 */ private String sender = null; /** addressee 邮件接收者 */ private String addressee = null; /** subject 邮件主题 */ private String subject = null; /** text 邮件消息 */ private String text = null; public static void init(){ MailboxTypes = new HashMap<String,String>(); MailboxTypes.put("163","smtp.163.com"); MailboxTypes.put("139","smtp.139.com"); MailboxTypes.put("126","smtp.126.com"); MailboxTypes.put("qq", "smtp.qq.com"); MailboxTypes.put("live","smtp.live.cn"); MailboxTypes.put("msn","smtp.msn.com"); MailboxTypes.put("kum", "mail.kum.net.cn"); MailboxTypes.put("hotmail","smtp.hotmail.cn"); } /** * initialization 实例化类成员变量 */ private void initialization(String sender,String addressee,String subject,String text){ this.sender = sender; this.addressee = addressee; this.subject = subject; this.text = text; this.host = getHost(sender); //System.out.println("sender->"+this.sender+" | addressee->"+this.addressee+" | subject->"+this.subject+" | text->"+this.text+" | host->"+this.host); } /** * getHost 获取目标邮箱服务器类型 * * @param sender 是String类型,传入邮件发送者邮箱地址信息 * @return String 返回目标邮箱服务器类型 */ private String getHost(String sender){ String _host,_host_ = null; _host = sender.substring(sender.indexOf("@")+1,sender.indexOf(".")); if(MailboxTypes==null){ init(); } _host_ = MailboxTypes.get(_host); // System.out.println(_host+" <--> "+_host_); if(_host_==null){ //MailboxTypes.put(_host,"smtp."+_host+".com"); MailboxTypes.put(_host,"smtp."+sender.substring(sender.indexOf("@")+1,sender.length())); } return MailboxTypes.get(_host); } public void sendMail(String sender, String password, String addressee, String subject, String text) throws Exception { initialization(sender,addressee,subject,text); Properties props = System.getProperties(); { props.put("mail.smtp.host",this.host); props.put("mail.smtp.auth","true"); } ValidateAuther auther = new ValidateAuther(this.sender,password); Session session = Session.getDefaultInstance(props,auther); MimeMessage msg = new MimeMessage(session); InternetAddress fromAddr = new InternetAddress(this.sender); // 发送者邮箱地址 InternetAddress toAddr = new InternetAddress(this.addressee); // 接收者邮箱地址 msg.setFrom(fromAddr); msg.addRecipient(Message.RecipientType.TO, toAddr); /** * Message.RecipientType.TO -- 接收者 * Message.RecipientType.CC -- 抄送 * Message.RecipientType.BCC --秘密抄送者 */ msg.setSubject(this.subject); // 邮件主题 msg.setText(this.text); // 邮件信息 Transport.send(msg); // 发送邮件 } } /** * ValidateAuther 邮件验证类,验证邮件发送者信息 */ class ValidateAuther extends Authenticator { /** 邮件发送者 */ private String sender; /** 邮件接受者 */ private String password; /** * ValidateAuther 验证邮件发送者信息 * * @param userName 是String类型,传入邮件发送者账户 * @param password 是String类型,传入邮件发送者账户密码 */ public ValidateAuther(String sender, String password) { this.sender = sender; this.password = password; } /** * getPasswordAuthentication 验证邮件发送者账户信息的函数 * * @param userName 邮件发送者账户信息 * @param password 邮件发送者账户密码 * @return PasswordAuthentication 返回邮件发送者账户验证信息 */ @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication(sender, password); } }

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

扶摇的星河

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值