Reputation: 247
<property name="host" value="smtp.gmail.com"/>
<property name="username" value="[email protected]"/>
<property name="password" value="abc123456"/>
<property name="port" value="587" />
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.smtp.ssl.enable">true</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
Error I am getting: DEBUG SMTP RCVD: 530 5.7.0 Must issue a STARTTLS command first. wd6sm44638663pab.3 - gsmtp
DEBUG SMTP SENT: QUIT org.springframework.mail.MailSendException: Failed messages: javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. wd6sm44638663pab.3 - gsmtp ; message exception details (1) are: Failed message 1: javax.mail.MessagingException: 530 5.7.0 Must issue a STARTTLS command first. wd6sm44638663pab.3 - gsmtp
Already enabled STARTTLS but why i am getting this error again ?
Also when i change transport to smtps and port to SSL i am not getting any response it waits for long time.
Upvotes: 0
Views: 1596
Reputation: 3439
I think there is the problem with the jar may be you have not included the jar which is provided by the mail providers. so use that jar and include that into the class path and error will be resolved.
Upvotes: 1
Reputation: 29971
You've enabled both SSL and STARTTLS, that's not going to work. Pick one. If you're using Gmail on port 587, you want STARTTLS.
From your error message, it looks like your properties are being ignored. Check the rest of your configuration to make sure your property settings match up with the JavaMail Session you're using.
Upvotes: 0
Reputation: 10274
Use this code to send email from java,just call constructor of this class by passing the required parameters on button or anywhere else from where you want to send email.
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Properties;
import javax.mail.Authenticator;
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;
public class GMailSender {
public GMailSender(String host, final String from, final String pass, String to, String sub, String mess) throws Exception {
Properties props = System.getProperties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", host);
// props.put("mail.smtp.port", port);
Authenticator auth = new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(from, pass);
}};
Session session = Session.getInstance(props, auth);
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
message.setSubject(sub);
message.setText(mess);
Transport.send(message);
}
public static void main(String arg[]) throws Exception {
if(arg.length == 5) {
StringBuilder message = new StringBuilder();
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
String temp = "", subject;
System.out.print("Enter subject: ");
subject = br.readLine();
System.out.println("Enter the message (end it with a . on a single line):");
while((temp = br.readLine()) != null) {
if(temp.equals("."))
break;
message.append(temp+"\n");
}
System.out.println("Sending message...");
new GMailSender(arg[0], arg[1], arg[2], arg[3], subject, message.toString());
System.out.println("Sent the message.");
}
else System.err.println("Usage:\njava SendTextMail <host> <port> <from> <pass> <to>");
}
}
Upvotes: 0