Если вы пытаетесь отправить почту через javax.mail API, вы можете использовать это
import java.util.Properties;
import javax.mail.Authenticator;
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 SendMailToMyself
{
private static final String SMTP_HOST_NAME = "smtp.gmail.com";
private static final String MY_EMAIL = "youremailID@gmail.com";
/**
* @param emailContact : Email of the person who contact you or From whom the email came.
* @param subject : Subject of the Email send by the contact.
* @param msgFromContact : Message Text of the Body.
*
* The method is used to take EmailID of the Contact, Subject of the Message,
* Message Text as the input, as provided on the JSP side Contact Me page and
* sends it to the Administrator of the Website on his Mail Address.
*/
public void postMail(String emailContact, String subject, String msgFromContact)
throws MessagingException
{
boolean debug = false;
// Set the host smtp address
Properties prop = new Properties();
prop.put("mail.smtp.host", SMTP_HOST_NAME);
/*
* Do remember to remove the below line from comment, if your mail server does support TLS (port 587), SSL(port 465) security features.
* Like if you sending a mail to Hotmail or gmail this must be uncommented, and then you have to use above ports
* instead of port 25.
*/
prop.put("mail.smtp.starttls.enable", "true");
prop.put("mail.smtp.port", "587");
prop.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(prop, auth);
session.setDebug(debug);
// Create a message.
Message msg = new MimeMessage(session);
// Set the from and to address.
InternetAddress addressFrom = new InternetAddress(emailContact);
msg.setFrom(addressFrom);
InternetAddress addressTo = new InternetAddress(MY_EMAIL);
msg.setRecipient(Message.RecipientType.TO, addressTo);
// Setting the subject and content Type
msg.setSubject(subject);
msg.setContent(msgFromContact, "text/plain");
Transport.send(msg);
}
public static void main(String... args) throws MessagingException
{
SendMailToMyself smtm = new SendMailToMyself();
smtm.postMail("sender@email.com", "Testing Program", "Hello there, Testing command prompt messaging.");
System.out.println("Your Message has been send. Regards");
}
}
А вот и SMTPAuthenticator Class
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
/**
* SimpleAuthenticator is used to do simple authentication
* when the SMTP server requires it.
*/
public class SMTPAuthenticator extends Authenticator
{
private static final String SMTP_AUTH_USER = "youremail@gmail.com";
private static final String SMTP_AUTH_PASSWORD = "yourpassword";
public PasswordAuthentication getPasswordAuthentication()
{
String username = SMTP_AUTH_USER;
String password = SMTP_AUTH_PASSWORD;
return new PasswordAuthentication(username, password);
}
}
Надеюсь, что это может помочь.
Привет