2012-02-27 106 views
5

我已經提到了以前對類似問題的回答,但它舉例說明了使用Gmail的傳出服務器配置。 不過,我想使用JavaMail API的Web郵件,我公司使用的是:使用JavaMail API在Android中發送電子郵件

發送服務器:smtp.softcellindia.com

端口:

的加密類型:none

我試過使用下面的代碼。但它似乎沒有發送郵件。

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"); 
     } 
    } 
} 
+0

我想你應該檢查你的服務器的SMTP端口。 – 2012-02-27 07:31:47

+0

我終於通過改變 props.setProperty(「** mail.host **」,mailhost); 至 props.setProperty(「** mail.smtp.host **」,mailhost); 並刪除以下行 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」); – 2012-03-01 08:36:33

+0

@Harshul ::你可以在這裏添加你自己的答案。我試圖使用相同的概念,但注意到異常:554消息被拒絕。 – Harpreet 2012-04-09 11:54:22

回答

0

我終於得到它的工作通過改變

props.setProperty("mail.host", mailhost);

props.setProperty("mail.smtp.host", mailhost);

和d選擇以下行

props.put("mail.smtp.socketFactory.port", "25"); 
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactor"); 
props.put("mail.smtp.socketFactory.fallback", "false"); 
props.setProperty("mail.smtp.quitwait", "false"); 
0

變化javax.net.ssl.SSLSocketFactoryjavax.net.SocketFactory

+0

是否適合你? – 2012-05-07 13:46:53

+1

發送服務器:smtp.yeah.net 端口:25 加密類型:無。可以成功發送郵件。 – fabs5068 2012-05-08 01:30:32

相關問題