使用JavaMail API在Android中发送电子邮件 [英] Sending Email in Android using JavaMail API

查看:312
本文介绍了使用JavaMail API在Android中发送电子邮件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经提到的previous回答我的类似的问题,但它给使用Gmail的邮件发送服务器配置的一个例子。 不过,我想使用JavaMail API来我公司正在使用的webmail:

发送服务器: smtp.softcellindia.com

端口: 25

加密类型:

我用下面的code都试过了。但它似乎不发送邮件。

 进口的javax.activati​​on.DataHandler;
进口javax.activati​​on.DataSource;
进口javax.mail.Message;
进口javax.mail.PasswordAut​​hentication;
进口javax.mail.Session的;
进口javax.mail.Transport;
进口javax.mail.internet.InternetAddress;
进口的javax.mail.internet.MimeMessage;
进口java.io.ByteArrayInputStream中;
进口java.io.IOException异常;
进口的java.io.InputStream;
进口java.io.OutputStream中;
进口java.security.Security中;
进口java.util.Properties;

公共类MailSender扩展javax.mail.Authenticator {
    私人字符串的邮件主机=smtp.softcellindia.com;
    私人字符串的用户;
    私人字符串密码;
    私人届会议;

    静态{
        Security.addProvider(新com.provider.JSSEProvider());
    }

    公共MailSender(用户字符串,字符串密码){
        this.user =用户;
        this.password =密码;

        属性道具=新特性();
        props.setProperty(mail.transport.protocol,SMTP);
        props.setProperty(mail.host,邮件主机);
        props.put(mail.smtp.auth,真);
        props.put(mail.smtp.port,25);
        props.put(mail.smtp.socketFactory.port,25);
        props.put(mail.smtp.socketFactory.class
                javax.net.ssl​​.SSLSocketFactory);
        props.put(mail.smtp.socketFactory.fallback,假);
        props.setProperty(mail.smtp.quitwait,假);

        会议=作为Session.getDefaultInstance(道具,这一点);

       }

    受保护的PasswordAut​​hentication的getPasswordAut​​hentication(){
        返回新的PasswordAut​​hentication(用户名,密码);
    }

    市民同步无效的sendmail(字符串学科,身体的字符串,字符串发件人,收件人字符串)抛出异常{
        尝试{
        的MimeMessage消息=新的MimeMessage(会议);
        DataHandler的处理程序=新的DataHandler(新ByteArrayDataSource(body.getBytes(),text / plain的));
        message.setSender(新网际(发件人));
        message.setSubject(学科);
        message.setDataHandler(处理);
        如果(recipients.indexOf(,)大于0)
            message.setRecipients(Message.RecipientType.TO,InternetAddress.parse(收件人));
        其他
            message.setRecipient(Message.RecipientType.TO,新的网际(收件人));
        Transport.send(消息);
        }赶上(例外五){

        }
    }

    公共类ByteArrayDataSource实现了数据源{
        私人byte []的数据;
        私人字符串类型;

        公共ByteArrayDataSource(byte []的数据,字符串类型){
            超();
            this.data =数据;
            this.type =类型;
        }

        公共ByteArrayDataSource(byte []的数据){
            超();
            this.data =数据;
        }

        公共无效的setType(字符串类型){
            this.type =类型;
        }

        公共字符串的getContentType(){
            如果(类型== NULL)
                返回应用程序/八位字节流;
            其他
                返回类型;
        }

        公众的InputStream的getInputStream()抛出IOException异常{
            返回新ByteArrayInputStream的(数据);
        }

        公共字符串的getName(){
            返回ByteArrayDataSource;
        }

        公众的OutputStream的getOutputStream()抛出IOException异常{
            抛出新的IOException异常(不支持);
        }
    }
}
 

解决方案

修改 javax.net.ssl​​.SSLSocketFactory javax.net.SocketFactory

I have referred to previous answers to similar question of mine but it gives an example using the outgoing server configuration of Gmail. However I want to use the JavaMail API for the webmail that my company is using:

Outgoing server: smtp.softcellindia.com

port: 25

Type of encryption: none

I have tried using the following code. But it seems to send no mails.

import javax.activation.DataHandler;   
import javax.activation.DataSource;   
import javax.mail.Message;   
import javax.mail.PasswordAuthentication;   
import javax.mail.Session;   
import javax.mail.Transport;   
import javax.mail.internet.InternetAddress;   
import javax.mail.internet.MimeMessage;   
import java.io.ByteArrayInputStream;   
import java.io.IOException;   
import java.io.InputStream;   
import java.io.OutputStream;   
import java.security.Security;   
import java.util.Properties;   

public class MailSender extends javax.mail.Authenticator {   
    private String mailhost = "smtp.softcellindia.com";   
    private String user;   
    private String password;   
    private Session session;   

    static {   
        Security.addProvider(new com.provider.JSSEProvider());   
    }  

    public MailSender(String user, String password) {   
        this.user = user;   
        this.password = password;   

        Properties props = new Properties();  
        props.setProperty("mail.transport.protocol", "smtp");   
        props.setProperty("mail.host", mailhost);   
        props.put("mail.smtp.auth", "true");   
        props.put("mail.smtp.port", "25");   
        props.put("mail.smtp.socketFactory.port", "25");   
        props.put("mail.smtp.socketFactory.class",   
                "javax.net.ssl.SSLSocketFactory");   
        props.put("mail.smtp.socketFactory.fallback", "false");   
        props.setProperty("mail.smtp.quitwait", "false");   

        session = Session.getDefaultInstance(props, this);

       }   

    protected PasswordAuthentication getPasswordAuthentication() {   
        return new PasswordAuthentication(user, password);   
    }   

    public synchronized void sendMail(String subject, String body, String sender, String recipients) throws Exception {   
        try{
        MimeMessage message = new MimeMessage(session);   
        DataHandler handler = new DataHandler(new ByteArrayDataSource(body.getBytes(), "text/plain"));   
        message.setSender(new InternetAddress(sender));   
        message.setSubject(subject);   
        message.setDataHandler(handler);   
        if (recipients.indexOf(',') > 0)   
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(recipients));   
        else  
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(recipients));   
        Transport.send(message);   
        }catch(Exception e){

        }
    }   

    public class ByteArrayDataSource implements DataSource {   
        private byte[] data;   
        private String type;   

        public ByteArrayDataSource(byte[] data, String type) {   
            super();   
            this.data = data;   
            this.type = type;   
        }   

        public ByteArrayDataSource(byte[] data) {   
            super();   
            this.data = data;   
        }   

        public void setType(String type) {   
            this.type = type;   
        }   

        public String getContentType() {   
            if (type == null)   
                return "application/octet-stream";   
            else  
                return type;   
        }   

        public InputStream getInputStream() throws IOException {   
            return new ByteArrayInputStream(data);   
        }   

        public String getName() {   
            return "ByteArrayDataSource";   
        }   

        public OutputStream getOutputStream() throws IOException {   
            throw new IOException("Not Supported");   
        }   
    }   
} 

解决方案

Change javax.net.ssl.SSLSocketFactory to javax.net.SocketFactory

这篇关于使用JavaMail API在Android中发送电子邮件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆