Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- TLS issue when sending to gmail through JavaMail
- Exception in thread "main" com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. l10sm302158wfk.21
- at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2057)
- at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1580)
- at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1097)
- at SendEmail.main(SendEmail.java:47)
- Properties props = new Properties();
- props.put("mail.transport.protocol", "smtp");
- props.put("mail.host", "smtp.gmail.com");
- props.put("mail.user", "blahblah@gmail.com");
- props.put("mail.password", "blah");
- props.put("mail.port", "587");
- Session mailSession = Session.getDefaultInstance(props, null);
- Transport transport = mailSession.getTransport();
- MimeMessage message = new MimeMessage(mailSession);
- message.setSubject("This is a test");
- message.setContent("This is a test", "text/plain");
- message.addRecipient(Message.RecipientType.TO, new InternetAddress("blahblah2@gmail.com"));
- transport.connect();
- transport.sendMessage(message, message.getRecipients(Message.RecipientType.TO));
- transport.close();
- props.put("mail.smtp.starttls.enable", "true");
- import java.util.*;
- import javax.mail.*;
- import javax.mail.internet.*;
- import javax.activation.*;
- public class CopyOfSendMail {
- private static String SMPT_HOSTNAME = "my smtp port no";
- private static String USERNAME = "root";
- private static String PASSWORD = "root";
- public static void main(String[] args) {
- Properties props = new Properties();
- props.put("mail.smtp.host", SMPT_HOSTNAME);
- props.put("mail.from","aaa@gmail.com");
- props.put("mail.smtp.starttls.enable", "true");
- props.put("mail.smtp.auth", "true");
- props.put("mail.debug", "true");
- Session session = Session.getInstance(props, new Authenticator() {
- @Override
- protected PasswordAuthentication getPasswordAuthentication() {
- return new PasswordAuthentication(USERNAME, PASSWORD);
- }
- });
- try {
- MimeMessage msg = new MimeMessage(session);
- msg.setFrom();
- msg.setRecipients(Message.RecipientType.TO,
- "bbb@gmail.com");
- msg.setSubject("JavaMail hello world example");
- msg.setSentDate(new Date());
- msg.setText("Hello, world!n");
- Transport.send(msg);
- } catch (MessagingException mex) {
- System.out.println("send failed, exception: " + mex);
- }
- }
- }
Add Comment
Please, Sign In to add comment