• java关于邮件发送


    很早就尝试学习一下邮件发送,但是老是不成功,报错javax.mail.AuthenticationFailedException

    该错误就是说邮箱认证失败,但是明明用户名,密码正确,同时开启了stmp邮件服务,但还是报错,百度了好多找不到解决办法,最后用foxmail尝试客户端登录,发现了问题,使用163邮箱,连不上,说明163不提供该服务,然后qq邮箱是开启stmp之后会有一个独立密码,在这里,使用stmp 的时候必须使用独立密码,新浪的好像可以,没试

    这里顺便贴一下,我结合网上的例子,写的发送邮件的代码

    邮件验证类

    import javax.mail.Authenticator;
    import javax.mail.PasswordAuthentication;
     
    /**
     *
     * ClassName: MyAuthenticator
     * @Description: 邮箱验证类,重写PasswordAuthentication方法
     * @author
     * @date 2015-10-13
     */
    public class MyAuthenticator extends Authenticator{
     private String userName;
     private String password;
     
     public String getUserName() {
      return userName;
     }
     
     public void setUserName(String userName) {
      this.userName = userName;
     }
     
     public String getPassword() {
      return password;
     }
     
     public void setPassword(String password) {
      this.password = password;
     }
     //重写
     protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(userName, password);
        }
     
     
    }
     
    2.class根目录下 的配置文件email.propertis

    mail.smtp.host=smtp.qq.com
    mail.smtp.port=25
    userName=*****@qq.com
    password=******
    mail.smtp.auth=true

     
    邮件发送类
     
    import java.io.File;
    import java.io.IOException;
    import java.io.InputStream;
    import java.util.Properties;

    import javax.activation.DataHandler;
    import javax.activation.DataSource;
    import javax.activation.FileDataSource;
    import javax.mail.Address;
    import javax.mail.BodyPart;
    import javax.mail.Message;
    import javax.mail.MessagingException;
    import javax.mail.Multipart;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.AddressException;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeBodyPart;
    import javax.mail.internet.MimeMessage;
    import javax.mail.internet.MimeMultipart;
    import javax.mail.internet.MimeUtility;

    import org.junit.Test;


    /**
     * 
     * ClassName: SendMail 
     * @Description: 邮箱发送类
     * @author 
     * @date 2015-10-13
     */
    public class SendMail {
    @Test
    public void sendMail() throws IOException{
    //接收者 的邮箱地址
    String to = "*****@qq.com";
    //邮件主题
    String subject = "test";
    //邮件内容
    String content = "www.baidu.com";
    //发送的附件
    File sendFile = new File("f://345.txt");
    //邮箱验证
     MyAuthenticator authenticator = null;
    //读取配置文件
    Properties properties = new Properties();
    //根据class获取配置文件的位置
    InputStream in = null;
    try {
    in = Object.class.getResourceAsStream("/email.propertis");
    properties.load(in);

    authenticator = new MyAuthenticator();
    authenticator.setUserName(properties.getProperty("userName"));
    authenticator.setPassword(properties.getProperty("password"));

    } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    }finally{
    if(in!=null){
    in.close();
    }
    }

    // 根据邮件会话属性和密码验证器构造一个发送邮件的session  
      Session sendMailSession = Session  
                    .getDefaultInstance(properties, authenticator);

            // 根据session创建一个邮件消息  
            Message mailMessage = new MimeMessage(sendMailSession);  

            // 创建邮件发送者地址  
            Address from;
            Address toUser;
    try {
    from = new InternetAddress(properties.getProperty("userName"));
    //输入发送者邮箱地址
            mailMessage.setFrom(from);
            
            // 创建邮件的接收者地址,并设置到邮件消息中  
                toUser = new InternetAddress(to);  
                mailMessage.setRecipient(Message.RecipientType.TO, toUser);
                
                //设置邮件消息的主题  
                mailMessage.setSubject(subject);
                //设置内容
                //mailMessage.setText(content);
                
                // 向multipart对象中添加邮件的各个部分内容,包括文本内容和附件
                Multipart multipart = new MimeMultipart();
                // 添加邮件正文
                BodyPart contentPart = new MimeBodyPart();
                contentPart.setContent(content, "text/html;charset=UTF-8");
                multipart.addBodyPart(contentPart);
                
                // 添加附件的内容
                BodyPart attachmentBodyPart = new MimeBodyPart();
                DataSource source = new FileDataSource(sendFile);
                attachmentBodyPart.setDataHandler(new DataHandler(source));
                    
                //MimeUtility.encodeWord可以避免文件名乱码
                attachmentBodyPart.setFileName(MimeUtility.encodeWord(sendFile.getName()));
                multipart.addBodyPart(attachmentBodyPart);
                
                // 将multipart对象放到message中
                mailMessage.setContent(multipart);
                // 保存邮件
                mailMessage.saveChanges();
                
                
                //发送邮件
                Transport.send(mailMessage);
    } catch (AddressException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
    } catch (MessagingException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();

           
    }

    }
     
    好了,邮箱发送就ok了
  • 相关阅读:
    Yii2中把路由地址中的%2F改为/
    深度解析常用的软件开发模型
    MYSQL索引的类型和索引的方式
    mysql errno 150
    士兵杀敌(五)
    stringstream字符串流
    士兵杀敌(二)(线段树+树状数组)
    士兵杀敌(一)(树状数组)
    C语言文件读写操作总结
    BC第二场
  • 原文地址:https://www.cnblogs.com/foreverRoad/p/4874220.html
Copyright © 2020-2023  润新知