android 使用JavaMail

Javamail-Android配置步骤:

  1. 下载Android版本JavaMail包,additional.jar、mail.jar和activation.jar,下载地址JavaMail-Android

  2. 在项目与src同一目录级别下,新建文件夹lib,将下载的3个jar包放入该文件夹

  3. 右键->Properties->Java Build Path->Libraries,选择Add External JARs,找到项目下lib目录的3个jar包

发送邮件类

Mail.java

package com.javamail;



import java.io.UnsupportedEncodingException;

import java.util.Date;

import java.util.Properties;



import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import com.javamail.common.MailAuthenticator;



public class Mail {

    private String host;//smtp服务器

    private String auth;//验证

    

    private String mailAcount;//邮件用户名

    private String password;//密码

    

    //共同变量

    private MimeMessage mimeMessage;

    private Properties props;

    private MailAuthenticator mailAuth;

    private Session session;

    /**

     * 构造函数

     */

    public Mail(String host,String mailAcount,String password){

        this.host=host;

        this.auth="true";

        this.mailAcount=mailAcount;

        this.password=password;

    }



    /**

     * 变量初始化

     */

    protected void initialize(){

        props = System.getProperties();

        // 指定smtp服务器

        props.put("mail.smtp.host", host);

        props.put("mail.smtp.auth", auth);

        // 新建一个包含smtp会话的MimeMessage对象

        session=Session.getDefaultInstance(props,null);

        mimeMessage=new MimeMessage(session);

    }

    

    /**

     * 设定邮件信息

     * @param from,to,subject

     * @throws MessagingException 

     * @throws AddressException 

     * @throws UnsupportedEncodingException 

     */

    public void create(String from,String to,String subject) throws AddressException, MessagingException, UnsupportedEncodingException{

        //初始化

        initialize();

        if (from.equals("") || to.equals("") ||subject.equals(""))

        {

            System.out.println("输入有误");

        }else{

            //指定送信人

            mimeMessage.setFrom(new InternetAddress(from));

            //对方邮件地址

            mimeMessage.setRecipients(Message.RecipientType.TO, to);

            //邮件标题

            mimeMessage.setSubject(subject,"GBK");

        }

    }

    

    /**

     * 邮件格式,和内容指定

     * @param content

     * @throws MessagingException 

     */

    public void addContent(String content) throws MessagingException{

        // 指定邮件格式

        mimeMessage.setHeader("Content-Type", "text/html");

        // 邮件内容

        mimeMessage.setText(content);

    }

    

    /**

     * 发信

     * @throws MessagingException

     */

    public void send() throws MessagingException{

// 指定送信日期

        mimeMessage.setSentDate(new Date());

        // 送信

        Transport transport = session.getTransport("smtp");

        transport.connect(host,mailAcount,password);

        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());

        transport.close();

    }

}
接收邮件类
Mail.java

package com.javamail;



import java.io.UnsupportedEncodingException;

import java.util.Date;

import java.util.Properties;



import javax.mail.Message;

import javax.mail.MessagingException;

import javax.mail.Session;

import javax.mail.Transport;

import javax.mail.internet.AddressException;

import javax.mail.internet.InternetAddress;

import javax.mail.internet.MimeMessage;

import com.javamail.common.MailAuthenticator;



public class Mail {

    private String host;//smtp服务器

    private String auth;//验证

    

    private String mailAcount;//邮件用户名

    private String password;//密码

    

    //共同变量

    private MimeMessage mimeMessage;

    private Properties props;

    private MailAuthenticator mailAuth;

    private Session session;

    /**

     * 构造函数

     */

    public Mail(String host,String mailAcount,String password){

        this.host=host;

        this.auth="true";

        this.mailAcount=mailAcount;

        this.password=password;

    }



    /**

     * 变量初始化

     */

    protected void initialize(){

        props = System.getProperties();

        // 指定smtp服务器

        props.put("mail.smtp.host", host);

        props.put("mail.smtp.auth", auth);

        // 新建一个包含smtp会话的MimeMessage对象

        session=Session.getDefaultInstance(props,null);

        mimeMessage=new MimeMessage(session);

    }

    

    /**

     * 设定邮件信息

     * @param from,to,subject

     * @throws MessagingException 

     * @throws AddressException 

     * @throws UnsupportedEncodingException 

     */

    public void create(String from,String to,String subject) throws AddressException, MessagingException, UnsupportedEncodingException{

        //初始化

        initialize();

        if (from.equals("") || to.equals("") ||subject.equals(""))

        {

            System.out.println("输入有误");

        }else{

            //指定送信人

            mimeMessage.setFrom(new InternetAddress(from));

            //对方邮件地址

            mimeMessage.setRecipients(Message.RecipientType.TO, to);

            //邮件标题

            mimeMessage.setSubject(subject,"GBK");

        }

    }

    

    /**

     * 邮件格式,和内容指定

     * @param content

     * @throws MessagingException 

     */

    public void addContent(String content) throws MessagingException{

        // 指定邮件格式

        mimeMessage.setHeader("Content-Type", "text/html");

        // 邮件内容

        mimeMessage.setText(content);

    }

    

    /**

     * 发信

     * @throws MessagingException

     */

    public void send() throws MessagingException{

// 指定送信日期

        mimeMessage.setSentDate(new Date());

        // 送信

        Transport transport = session.getTransport("smtp");

        transport.connect(host,mailAcount,password);

        transport.sendMessage(mimeMessage, mimeMessage.getAllRecipients());

        transport.close();

    }

}

最后,给出朋友们几个注意的地方:  
1、使用此代码你可以完成你的javamail的邮件发送功能、发多个邮箱。三个类缺一不可。  
2、这三个类我打包是用的com.util.mail包,如果不喜欢,你可以自己改,但三个类文件必须在同一个包中  
3、不要使用你刚刚注册过的邮箱在程序中发邮件,如果你的163邮箱是刚注册不久,那你就不要使用“smtp.163.com”。因为你发不出去。刚注册的邮箱是不会给你这种权限的,也就是你不能通过验证。要使用你经常用的邮箱,而且时间比较长的。  
4、另一个问题就是mailInfo.setMailServerHost("smtp.163.com");mailInfo.setFromAddress("xxx@163.com");这两句话。即如果你使用163smtp服务器,那么发送邮件地址就必须用163的邮箱,如果不的话,是不会发送成功的。  
5、关于javamail验证错误的问题,网上的解释有很多,但我看见的只有一个。就是我的第三个类。你只要复制全了代码,我想是不会有问题的。

6、 然后在Android项目中添加网络访问权限

<uses-permission android:name="android.permission.INTERNET"></uses-permission>


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
要在 Android使用 JavaMail 设置代理发送邮件,可以按照以下步骤进行操作: 1. 首先创建一个 Properties 对象,设置代理服务器的主机名和端口号: ``` Properties props = new Properties(); props.setProperty("mail.smtp.host", "your.smtp.proxy.host"); props.setProperty("mail.smtp.port", "your.smtp.proxy.port"); ``` 2. 接下来创建一个 Authenticator 对象,用于进行 SMTP 鉴权: ``` Authenticator auth = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your.username", "your.password"); } }; ``` 3. 创建一个 Session 对象,传入上面的 Properties 和 Authenticator 对象: ``` Session session = Session.getInstance(props, auth); ``` 4. 创建一个 MimeMessage 对象,设置邮件内容: ``` MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("Test Email"); message.setText("This is a test email."); ``` 5. 最后调用 Transport.send() 方法将邮件发送出去: ``` Transport.send(message); ``` 完整的代码示例: ``` Properties props = new Properties(); props.setProperty("mail.smtp.host", "your.smtp.proxy.host"); props.setProperty("mail.smtp.port", "your.smtp.proxy.port"); Authenticator auth = new Authenticator() { @Override protected PasswordAuthentication getPasswordAuthentication() { return new PasswordAuthentication("your.username", "your.password"); } }; Session session = Session.getInstance(props, auth); MimeMessage message = new MimeMessage(session); message.setFrom(new InternetAddress("sender@example.com")); message.setRecipients(Message.RecipientType.TO, InternetAddress.parse("recipient@example.com")); message.setSubject("Test Email"); message.setText("This is a test email."); Transport.send(message); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值