About java Mail Using Authentication
Hai
This is srikanth I need to how can i need a mail when stmp server needs Athentication Please do help in this
And this is the code what i have tried and this is working in local system
<%@ page import="javax.mail.*" %>
<%@ page import="javax.mail.internet.*" %>
<%@ page import="javax.mail.MessagingException" %>
<%@ page import="java.io.UnsupportedEncodingException" %>
<%@ page import="java.util.Properties" %>
<%@ page import="javax.servlet.http.*" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.IOException" %>
<%@ page import="java.io.*" %>
<%! String from,to,sub,mes;
public void sendMail(String netuserid,String netpass,String from,String to,String sub,String mes)
{
try
{
// If either init parameter or request
// parameter "debug" is set, output
// SMTP session1 log to server log.
boolean debug = false;
Properties prop = new Properties();
prop.put("mail.host", "smtpout.secureserver.net");
prop.put("mail.user", netuserid);
prop.put("mail.password", netpass);
prop.put("mail.smtp.auth", "true");
javax.mail.Session session1 = javax.mail.Session.getDefaultInstance(prop, null);
// Create new message
MimeMessage msg = new MimeMessage(session1);
// Put data from request into message
String messageText = mes;
msg.setText(messageText);
String subject = sub;
if (subject != null) {
msg.setSubject(subject);
}
String addrFrom =
from;
String nameFrom =
from;
Address fromAddr =
new InternetAddress(addrFrom, nameFrom);
msg.setFrom(fromAddr);
String addrto = to;
String nameto = to;
Address toAddr =
new InternetAddress(addrto, nameto);
msg.addRecipient(Message.RecipientType.TO, toAddr);
javax.mail.Session _mailSession = javax.mail.Session.getDefaultInstance(prop, null);
// Send the message
//Transport.send(msg);
Transport tr = _mailSession.getTransport("smtp");
tr.connect("smtpout.secureserver.net", netuserid, netpass);
msg.saveChanges();
tr.sendMessage(msg, msg.getAllRecipients());
tr.close();
// Print a message acknowledging that the message
// was sent
}
catch(Exception e)
{
System.out.println("Problem " + e);
}
}
%>
<%
from="srekanth15@yahoo.com";
to="srikanth@yes-vsolutions.com";
sub="test mail from godaddy web space";
mes="Hai how do u this id test mail from godaddy web space";
out.println(sub);
out.println(mes);
out.println(to);
out.println(from);
%>
<%
sendMail("yesvsolutions","yesvsolutions","support@yes-vsolutions.com",to,sub,mes);
%>
When it is deployed in server this is working Please do help me is the problem with the space provider are With the code
hi,
i saw your post ,even i have small problem related to javamail authentication.
I have attached my code below.The work that i want the code to do is when the user wants to send a mail the user should be able to authenticate himself by giving the user name and password and that user name should be verified..but the code that i have attached below gets the password and id but does not verfy it and directly sends a mail.can u please tell me whats wrong with my program.
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
import java.io.*;
import javax.swing.*;
public class SendMailUsingAuthentication
{
private static final String SMTP_HOST_NAME = "smtp.net4india.com";
private static final String SMTP_AUTH_USER = "amna";
private static final String SMTP_AUTH_PWD = "servlets";
private static final String emailMsgTxt= "Online Order Confirmation Message. Also include the Tracking Number.";
private static final String emailSubjectTxt = "Order Confirmation Subject";
private static final String emailFromAddress = "dasdasdd@advicesync-consulting.com";
// Add List of Email address to who email needs to be sent to
private static final String[] emailList = {"amna_1981@rediffmail.com", "abid_akhtar2005@yahoo.com"};
public static void main(String args[]) throws Exception
{
SendMailUsingAuthentication smtpMailSender = new SendMailUsingAuthentication();
smtpMailSender.postMail( emailList, emailSubjectTxt, emailMsgTxt, emailFromAddress);
System.out.println("Sucessfully Sent mail to All Users");
}
public void postMail( String recipients[ ], String subject,
String message , String from) throws MessagingException
{
//boolean debug = false;
//Set the host smtp address
Properties props = new Properties();
props.put("mail.smtp.host", SMTP_HOST_NAME);
props.put("mail.smtp.auth", "true");
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getDefaultInstance(props, auth);
session.setDebug(true);
// create a message
Message msg = new MimeMessage(session);
// set the from and to address
InternetAddress addressFrom = new InternetAddress(from);
msg.setFrom(addressFrom);
InternetAddress[] addressTo = new InternetAddress[recipients.length];
for (int i = 0; i < recipients.length; i++)
{
addressTo = new InternetAddress(recipients);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
private class SMTPAuthenticator extends javax.mail.Authenticator
{
public PasswordAuthentication getPasswordAuthentication()
{
String username, password;
String result = JOptionPane.showInputDialog(
"Enter 'username,password'");
StringTokenizer st = new StringTokenizer(result, ",");
username = st.nextToken();
password = st.nextToken();
return new PasswordAuthentication(username,password);
}
}
}
regards
amna