2012-02-27 3 views
5

이전 질문에 대한 답변을 언급했지만 Gmail의 발신 서버 구성을 사용한 예를 보여줍니다. 그러나 나는 내 회사가 사용하고있는 웹 메일은 JavaMail API를 사용하려면 :JavaMail API를 사용하여 Android에서 이메일 보내기

보내는 서버 : smtp.softcellindia.com

포트 : 암호화 25

유형 : 없음

다음 코드를 사용해 보았습니다. 그러나 그것은 아무 우편도 보내지 않는 것처럼 보인다.

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 포트를 확인해야한다고 생각합니다. –

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

+0

@Harshul :: UR 자신의 답변을 추가 할 수 있습니까? 나는 같은 개념을 사용하려하지만 예외를 얻고있다 : 554 메시지가 거부되었다. – Harpreet

답변

0

에 나는 마지막으로 그것은

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

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

와 D를 변경하여 작업있어했다 eleted following lines

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

당신과 함께 할 수 있습니까? –

+1

보내는 서버 : smtp.yeah.net 포트 : 25 암호화 유형 : 없음. 메일을 성공적으로 보낼 수 있습니다. – fabs5068

관련 문제