-
Java mail API
hello friends,
i m new to java,
I just want to use a java mail API to send an email, but without authentication, i tried to set and unset authentication properties and tested but without authentication it's not working. My program is working fine with authentication but I just want it without authentication, so can i get some help,
Below i am posting my email sending code, which uses a SMTP server to send an email....
.................................................. .................................................. ..........
/*
* Program to send a single file using java mail API.
*/
Code:
package com.email;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Date;
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.PasswordAuthentication;
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;
public class Email
{
private String to;
private String subject;
private String body;
static Properties properties = new Properties();
static
{
properties.put("mail.smtp.port", "465");
properties.put("mail.smtp.auth", "true");
properties.put("mail.smtp.socketFactory.port", "465");
properties.put("mail.smtp.host","ourHostName.com");
properties.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
}
public static void main(String[] args) throws IOException
{
String message, messageBody = "";
Email email = new Email();
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("To: ");
String to = in.readLine();
email.setTo(to);
System.out.print("Subject: ");
String subject = in.readLine();
email.setSubject(subject);
System.out.println("Type \"stop\" to end-up with writing a message body:");
while (!((message = in.readLine()).equals("stop")))
{
messageBody += message;
}
email.setBody(messageBody);
String status = email.sendMail(to, messageBody);
System.out.println(status);
}
public String sendMail(String to, String bodyText) throws IOException {
System.out.println("Trying to send a mail.\nAuthenticating.");
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
try {
Session session = Session.getDefaultInstance(properties,
new javax.mail.Authenticator() {
protected PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(
"ourEmailId@domainName.com",
"password");
}
});
System.out.println("Authenticated.");
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress("admin@domainName.com"));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(to));
message.setSubject(subject);
MimeBodyPart textPart = new MimeBodyPart();
textPart.setText(" " + bodyText);
Multipart multiPart = new MimeMultipart();
multiPart.addBodyPart(textPart);
MimeBodyPart filePart = new MimeBodyPart();
System.out.print("Enter file name to attch in an email:");
String fname = in.readLine();
DataSource source = new FileDataSource(fname);
filePart.setDataHandler(new DataHandler(source));
filePart.setFileName(fname);
multiPart.addBodyPart(filePart);
message.setContent(multiPart);
message.setSentDate(new Date());
Transport.send(message);
System.out.println("File also sent successfully.");
}
catch (MessagingException e)
{
e.printStackTrace();
return " Mail sending failed.";
}
return "Your mail is sent successfully.";
}
public String getTo()
{
return to;
}
public void setTo(String to)
{
this.to = to;
}
public String getBody()
{
return body;
}
public void setBody(String body)
{
this.body = body;
}
public static Properties getProperties()
{
return properties;
}
public static void setProperties(Properties properties)
{
Emailer.properties = properties;
}
public String getSubject()
{
return subject;
}
public void setSubject(String subject)
{
this.subject = subject;
}
}
.................................................. ..................................
:(punch):
And if there are any technical reasons behind the scenes, let me know...please.
-
Re: Java mail API
-
Re: Java mail API
I assume you are you sure your mail server actually supports mail without authentication?
Also what exact error occurs then when you try? Post the complete error message.
-
Re: Java mail API
Actually i am not getting any errors according to above code when i used to set authentication property but when i tried it with the following property: properties.put("mail.smtp.auth", "false"); and created a session object like this:- Session session = Session.getDefaultInstance(properties, null); i expect it should work but it's not....
And as u said what errors occurs when i used it without authentication then following errors comes...
javax.mail.AuthenticationFailedException
Mail sending failed.
at javax.mail.Service.connect(Service.java:306)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at com.email.Email.sendMail(Email.java:112)
at com.email.Email.main(Email.java:66)
-
Re: Java mail API
sagarsbodage, I asked you to edit your post. Were you unable to understand why, or are you just lazy?
db