Issue to send mail? - java

i created the application for mail sending in java Spring, the mail sending properly working in local server. after i export the (war file ) and host into tomcat server then execute the app but mail was not sending i don't know what is the problem of following code?
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 SendMail {
public static void main(String[] args) {
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "465");
props.put("mail.smtp.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "465");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username","password");
}
});
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress("----#gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("----#gmail.com"));
message.setSubject("Test");
message.setText("Hello");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}

i got the answer,it's nothing but the port numbers is problem, I'm change and use this two port number 25 (or) 587.

Related

How i can add mail.jar to oracle sql developer to solve this error

I have looked at many postings on the ( I need to add the mail.jar and activation.jar to oracle Sql developer ):
"javax.mail.NoSuchProviderException:No provider for Address type: rfc822"...
I have written other classes in java that are used by Oracle Forms6i, but can't get this email portion to work. The code that seems to be failing is:
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 EnvoyerEmail {
private String username = "xxxxxxx#gmail.com";
private String password = "****";
public void envoyer() {
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("xxxxx#gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("yyyyyy#gmail.com"));
message.setSubject("Test email");
message.setText("Bonjour, ce message est un test ...");
Transport.send(message);
System.out.println("Message_envoye");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
public static void main(String[] args) {
EnvoyerEmail test = new EnvoyerEmail();
test.envoyer();
}
}
But I keep getting the error mentioned above when it tries to send it. I assure you that I have the mail.jar, activation.jar, pop3.jar, mailapi.jar, smtp.jar, imap.jar in the class path. So that is not the problem.
I was suggested on an Oracle Forum that this will need to be signed. I'm very ignorant of this (the java experience that I have is with JSP), but thought that I got things "secured/signed", but I still keep getting the same problem.

I want to turn a main method into a new non-main method class

I have a program that I can send emails through. However, this needs to be a part of a much bigger program. The Email class is under a different package whereas my other 2 classes (The driver class/main program, as well as another object class) are both in the default package. Can I access the email class despite it being in a different package or do I need them all to be in one package? And how do I go about doing either of these? Currently, I tried removing the main method part of the email class and putting it in the default package with my driver class, this resulted in many syntax errors. Below are some photos showing my classes and some code. The SendMail class is the same as SendMailTLS just with the main method being removed and put into the default package. The SendMailTLS class works perfectly, I just need to be able to access it from the IA class.
SendMail Class:
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 SendMail {
final String username = "treybyroncollier#gmail.com";
final String password = "13october";
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("treybyroncollier#gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("treycollier#live.co.uk"));
message.setSubject("THIS EMAIL IS A TEST");
message.setText("Hello Trey, just to let you know that this email is a test and everything is working with Java.");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
SendMailTLS Class:
package com.mkyong.common;
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 = "treybyroncollier#gmail.com";
final String password = "13october";
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("treybyroncollier#gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("treycollier#live.co.uk"));
message.setSubject("THIS EMAIL IS A TEST");
message.setText("Hello Trey, just to let you know that this email is a test and everything is working with Java.");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
You should learn the basics first and then start with such a comlicated program.
In your sendMail class, you added all of your code directly into the class body, that's not going to work. Instead, create a method in that class and paste your code in there.
Then you can call that method from your other class after you imported the package.
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 SendMail {
public static void start() {
final String username = "treybyroncollier#gmail.com";
final String password = "13october";
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("treybyroncollier#gmail.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse("treycollier#live.co.uk"));
message.setSubject("THIS EMAIL IS A TEST");
message.setText("Hello Trey, just to let you know that this email is a test and everything is working with Java.");
Transport.send(message);
System.out.println("Done");
} catch (MessagingException e) {
throw new RuntimeException(e);
}
}
}
You can then run that code from your new main method or any other method you like.
public class YourMainClass {
public static void main(String[] args) {
SendMail.start();
}
}
In short, if you don't want your main method to execute when you start the program, just change its name from main to something of your choice and remove the parameter String[] args.

javax.mail.MessagingException: Could not convert socket to TLS exception in JAVA

I want to send a mail from my email address to another email address via clicking a button in JFrame netbeans.Here is the code,
import java.awt.HeadlessException;
import java.util.Properties;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;
import javax.swing.JOptionPane;
import com.sun.mail.util.MailSSLSocketFactory;
import java.io.File;
import javax.mail.PasswordAuthentication;
import javax.mail.*;
import javax.swing.JFileChooser;
private void btn_sendActionPerformed(java.awt.event.ActionEvent evt) {
final String From = txt_from.getText();
final String password = txt_password.getText();
String To = txt_to.getText();
String subject = txt_sub.getText();
String txtmessage = txt_body.getText();
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.port", "587");
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLsocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "587");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.ssl.trust", "smtp.gmail.com");
props.put("mail.smtp.socketFactory.fallback", "true");
props.put("mail.smtp.ssl.socketFactory", "true");
props.put("mail.smtp.EnableSSL.enable","true");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(From, password);
}
}
);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(From));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(To));
message.setSubject(subject);
MimeBodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setText(txtmessage);
Multipart multiPart = new MimeMultipart();
multiPart.addBodyPart(messageBodyPart);
messageBodyPart = new MimeBodyPart();
DataSource source = new FileDataSource(attachment_path);
messageBodyPart.setDataHandler(new DataHandler(source));
messageBodyPart.setFileName(txt_DocAttach.getText());
multiPart.addBodyPart(messageBodyPart);
message.setContent(multiPart);
Transport.send(message);
JOptionPane.showMessageDialog(rootPane, "Message is sent");
} catch (MessagingException | HeadlessException e) {
JOptionPane.showMessageDialog(rootPane, e);
}
}
But it is giving me following error,
javax.mail.MessagingException: Could not convert socket to TLS; nested exception is: java.io.lOException: Exception in startTLS using SSL socket factory class null: host, port smtp.gmail.com, 587; Exception: java.lang.ClassNotFoundException: javax.netssl.SSLsocketFactory
Tried a lot but can't figure out what to do to solve it? Help please.
Try to change the value of the property mail.smtp.socketFactory.class
to javax.net.ssl.SSLSocketFactory instead of javax.net.ssl.SSLsocketFactory,
The class name is case sensitive.
For more infromations about connection properties you can look at:
Where to find all available Java mail properties?
As others have pointed out, your socket factory settings were wrong.
More importantly, you never needed to set them at all!
This error can also pop up if your JavaMail lib (mail.jar or javax.mail.jar) is too old.
Download the newest version from here: https://javaee.github.io/javamail/

getting error while sending mail from glassfish server

i have glass fish server i am trying to send mail through that server. but getting an error.
CODE as below
import java.util.Properties;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
import javax.mail.Message;
import javax.mail.MessagingException;
public class EmailService{
public static void main(String[] args) {
String to="to#gmail.com";//change accordingly
String from="from#";//change accordingly
String password="password";//change accordingly
System.out.println("started");
Properties props = new Properties();
props.put("mail.smtps.host", "localhost");
props.put("mail.smtps.socketFactory.port", "8080");
props.put("mail.smtps.socketFactory.class",
"javax.net.ssl.SSLSocketFactory");
props.put("mail.smtps.auth", "true");
props.put("mail.transport.protocol", "smtps");
props.put("mail.smtps.connectiontimeout", "90000");
props.put("mail.smtps.timeout", "90000");
Session session = Session.getDefaultInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication("username","password");
}
});
try {
System.out.println("started");
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress("to"));
message.setSubject("Nothing Special..");
message.setText("Send Mail By Java Programmm....");
System.out.println("started");
//send message
System.out.println(message);
Transport.send(message);
System.out.println("started");
System.out.println("message sent successfully");
} catch (MessagingException e) {throw new RuntimeException(e);}
}
}
But i am getting an error. i have changed the port details. created Javamail session is glassfishserver as well.
javax.mail.internet.MimeMessage#6daa8eb7
Exception in thread "main" java.lang.RuntimeException: javax.mail.MessagingExcep
tion: Could not connect to SMTP host: localhost, port: 25;
nested exception is:
java.net.ConnectException: Connection refused: connect

Java Mail Error In Sending Mails

I am using a program to send emails. The code works when I use some other mail server. but I need to use my company's email account to send email. And the email account is provided by gmail xxxx#companyname.com. When I change the mail host to `stmp.gmail.com, I encounter the following error:
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. st6sm11092256pbc.58
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:1515)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1054)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:634)
at javax.mail.Transport.send0(Transport.java:189)
at javax.mail.Transport.send(Transport.java:118)
at Mail.sendMail(Mail.java:48)
at Test.main(Test.java:6)
The code is as follows
import javax.mail.Authenticator;
import javax.mail.PasswordAuthentication;
public class Email_Autherticator extends Authenticator {
String username = "xxxx#gmail";
String password = "xxxxx";
public Email_Autherticator() {
super();
}
public Email_Autherticator(String user,String pwd){
super();
username = user;
password = pwd;
}
public PasswordAuthentication getPasswordAuthentication(){
return new PasswordAuthentication(username,password);
}
}
import java.util.Date;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.SendFailedException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class Mail {
private String host = "smtp.gmail.com";
private String mail_head_name = "this is head of this mail";
private String mail_head_value = "this is head of this mail";
private String mail_to = "xxxx#gmail.com";
private String mail_from = "xxxx#Comanyname.com";//using gmail server
private String mail_subject = "this is the subject of this test mail";
private String mail_body = "this is mail_body of this test mail";
private String personalName = "xxx";
public void sendMail() throws SendFailedException{
try {
Properties props = new Properties();
Authenticator auth = new Email_Autherticator();
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true");
System.out.println(props);
Session session = Session.getDefaultInstance(props,auth);
MimeMessage message = new MimeMessage(session);
message.setContent("Hello","text/plain");
message.setSubject(mail_subject);
message.setText(mail_body);
message.setHeader(mail_head_name, mail_head_value);
message.setSentDate(new Date());
Address address = new InternetAddress(mail_from,personalName);
message.setFrom(address);
Address toaddress = new InternetAddress(mail_to);
message.addRecipient(Message.RecipientType.TO,toaddress);
System.out.println(message);
Transport.send(message);
System.out.println("Send Mail Ok!");
} catch (Exception e) {
e.printStackTrace();
}
//return flag;
}
}
You almost certainly just need to rework your code to add the properties defined in the JavaMail API - Sending email via Gmail SMTP example example.
You can probably get away with setting your props to this:
Properties properties = new Properties();
properties.setProperty("mail.smtp.auth", "true");
properties.setProperty("mail.smtp.starttls.enable", "true");
properties.setProperty("mail.smtp.host", "smtp.gmail.com");
properties.setProperty("mail.smtp.port", "587");
properties.setProperty("mail.smtp.user", gmailUsername);
properties.setProperty("mail.smtp.password", gmailPassword);
As this seems to be for work--if you can--I suggest using Spring. It makes it a lot cleaner and easier to use. I just recently did something similar to this with Spring and Gmail SMTP.
You need to define below properties in your java code.
import below package
import java.util.Properties;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
and add following to your code:
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.host", host);
props.put("mail.smtp.port", "587");
Use javax.mail.authenticator to authenticate with gmail servers
Session session = Session.getInstance(props,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(username, password);
}
});
Try this should work for you.
If still having issues, refer this error doc:
https://pepipost.com/tutorials/common-javamail-smtp-errors/
for sending SMTP email using javaMail API

Categories