2012-12-01 7 views
6

Come posso cambiare il metodo setFrom() con quello che voglio? Posso inviare e-mail tramite il mio account Gmail e modificare il setFrom del testo, ma mostra il mio username per l'e-mail. Ho provato a usare anche il mio account yahoo e ottengo un errore di autenticazione.API Javamail - Come si cambia setFrom con quello che si desidera?

voglio cambiare l'indirizzo del mittente. Il codice è il seguente:

import java.util.Properties; 

import javax.mail.Message; 
import javax.mail.MessagingException; 
import javax.mail.PasswordAuthentication; 
import javax.mail.Session; 
import javax.mail.Transport; 
import javax.mail.internet.InternetAddress; 
import javax.mail.internet.MimeMessage; 

public class SendMailTLS { 

    public static void main(String[] args) { 

     final String username = "[email protected]"; 
     final String password = "password"; 

     Properties props = new Properties(); 
     props.put("mail.smtp.auth", "true"); 
     props.put("mail.smtp.starttls.enable", "true"); 
     props.put("mail.smtp.host", "smtp.gmail.com"); 
     props.put("mail.smtp.port", "587"); 

     Session session = Session.getInstance(props, 
      new javax.mail.Authenticator() { 
       protected PasswordAuthentication getPasswordAuthentication() { 
        return new PasswordAuthentication(username, password); 
       } 
      } 
     ); 

     try { 

      Message message = new MimeMessage(session); 
      message.setFrom(new InternetAddress("[email protected]")); 
      message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse("[email protected]")); 
      message.setSubject("Testing Subject"); 
      message.setText("Dear Mail Crawler," 
       + "\n\n No spam to my email, please!"); 

      Transport.send(message); 

      System.out.println("Done"); 

     } catch (MessagingException e) { 
      throw new RuntimeException(e); 
     } 
    } 
} 
+0

Non utilizzare la staffetta di Google? – Perception

+2

Amico mio, stai utilizzando il relay di Google per inviare la tua posta ('props.put (" mail.smtp.host "," smtp.gmail.com ");'). Utilizzare un altro relè se si desidera applicare meno rigorosamente il nome utente visualizzato come indirizzo. – Perception

+0

Quello che stai cercando di fare è comunemente chiamato "spam". :-) Ecco perché Gmail non ti permetterà di farlo. Supponendo di avere un motivo legittimo per fare questo, si veda questa pagina di aiuto di Gmail: [Invio di posta da un indirizzo diverso] (https://support.google.com/mail/bin/answer.py?hl=en&answer=22370) –

risposta

0

Molti relay SMTP stimabile non vi permetterà di falsificare la propria identità (che renderebbe ancora più facile per gli spammer di abusare del servizio). Detto questo, se il tuo obiettivo è quello di evitare più mail nella tua casella di posta, Google allows you to modify your email address in modo da poter filtrare eventuali risposte alla tua email.

-2

Ecco il codice completo È possibile utilizzare il seguente codice. Questo sta lavorando bene per me

class SendMail { 
public static void main(String[] args) { 
    System.out.println("In side main()---------------------"); 

    Properties props = new Properties(); 


    props.put("mail.smtp.host", "hostname"); 
    props.put("mail.smtp.port", "port");// 

    props.put("mail.smtp.socketFactory.port", "port"); 
    props.put("mail.smtp.socketFactory.class", 
      "javax.net.ssl.SSLSocketFactory"); 



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

    Session session = Session.getDefaultInstance(props, 
      new javax.mail.Authenticator() { 

       protected PasswordAuthentication getPasswordAuthentication() { 
        System.out 
          .println("In side getPasswordAuthentication------------------And Before returning PasswordAuthentication"); 
        return new PasswordAuthentication("[email protected]", 
          "password"); 

       } 

      }); 
    System.out 
      .println("mail and password has been sent********************"); 
    try { 
     System.out 
       .println("we are in try{} block................................."); 
     Message message = new MimeMessage(session); 
     message.setFrom(new InternetAddress("[email protected]")); 
     message.setRecipients(Message.RecipientType.TO, 
       InternetAddress.parse("[email protected]")); 
     message.setSubject("Testing Subject"); 
     message.setText("Dear User," + "\n\n This is testing only!"); 

     Transport.send(message); 

     System.out 
       .println("Mail has been sent successfully.........................."); 

    } catch (MessagingException e) { 
     System.out.println("we are in catch block..................."); 
     e.printStackTrace(); 

    } 
} 

}

+0

http://www.oracle.com/technetwork/java/faq-135477.html#commonmistakes – BalusC

1

fronte Stesso problema durante l'utilizzo di "smtp.gmail.com". Usa Mandrill, funzionerà. Dopo aver configurato un account Mandrill, utilizzare "smtp.mandrillapp.com" sulla porta 587. Per l'autenticazione, impostare username = il proprio nome utente e password mandrill = chiave API generata nel proprio account.

Problemi correlati