Results 1 to 20 of 21
Thread: Using java send email
- 01-19-2011, 03:04 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 33
- Rep Power
- 0
- 01-19-2011, 03:09 PM #2
- 01-19-2011, 03:10 PM #3
- 01-19-2011, 03:33 PM #4
Member
- Join Date
- Nov 2010
- Posts
- 33
- Rep Power
- 0
this is inside my gui listerner.
String subject = "Chit Chit recovery";
String message = "Your username is " + u1.getUserName() + "Your password is "
+ u1.getPwd()+ ". Thank you for using Chit Chit";
String from = "guoxiang1610@gmail.com";
Email e1 = new Email();
e1.sendEmail(from, email, subject, message);
- 01-19-2011, 03:33 PM #5
Member
- Join Date
- Nov 2010
- Posts
- 33
- Rep Power
- 0
this is inside my gui listerner.
String subject = "Chit Chit recovery";
String message = "Your username is " + u1.getUserName() + "Your password is "
+ u1.getPwd()+ ". Thank you for using Chit Chit";
String from = "guoxiang1610@gmail.com";
Email e1 = new Email();
e1.sendEmail(from, email, subject, message);
- 01-19-2011, 03:34 PM #6
Member
- Join Date
- Nov 2010
- Posts
- 33
- Rep Power
- 0
public void sendEmail(
String aFromEmailAddr, String aToEmailAddr,
String aSubject, String aBody
){
//Here, no Authenticator argument is used (it is null).
//Authenticators are used to prompt the user for user
//name and password.
Session session = Session.getDefaultInstance( fMailServerConfig, null );
MimeMessage message = new MimeMessage( session );
try {
//the "from" address may be set in code, or set in the
//config file under "mail.from" ; here, the latter style is used
//message.setFrom( new InternetAddress(aFromEmailAddr) );
message.addRecipient(
Message.RecipientType.TO, new InternetAddress(aToEmailAddr)
);
message.setSubject( aSubject );
message.setText( aBody );
Transport.send( message );
}
catch (MessagingException ex){
System.err.println("Cannot send email. " + ex);
}
}
public static void refreshConfig() {
fMailServerConfig.clear();
fetchConfig();
}
// PRIVATE //
private static Properties fMailServerConfig = new Properties();
static {
fetchConfig();
}
/**
* Open a specific text file containing mail server
* parameters, and populate a corresponding Properties object.
*/
private static void fetchConfig() {
InputStream input = null;
try {
//If possible, one should try to avoid hard-coding a path in this
//manner; in a web application, one should place such a file in
//WEB-INF, and access it using ServletContext.getResourceAsStream.
//Another alternative is Class.getResourceAsStream.
//This file contains the javax.mail config properties mentioned above.
input = new FileInputStream( "C:\\Temp\\MyMailServer.txt" );
fMailServerConfig.load( input );
}
catch ( IOException ex ){
System.err.println("Cannot open and load mail server properties file.");
}
finally {
try {
if ( input != null ) input.close();
}
catch ( IOException ex ){
System.err.println( "Cannot close mail server properties file." );
}
}
}
this is my email class
- 01-19-2011, 03:55 PM #7
Seems like you have stored the SMTP host details inside your MyMailServer.txt file. What I am interested to know is that, the SMTP host address provided by you is running or not?
Your original error "Could not connect to SMTP host." is may be out of no connectivity to SMTP. Can you ping to that host and confirm?
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-19-2011, 05:00 PM #8
Member
- Join Date
- Nov 2010
- Posts
- 33
- Rep Power
- 0
and how do i do that?sorry i am new to java
- 01-20-2011, 06:48 AM #9
Most probably your MyMailServer.txt file must be containing some host address. Something like,
Java Code:mail.host = some_address OR mail.smtp.host = some_address
You need to first validate that it's a valid SMTP server, which can actually 'send' your mail outside. Then you need to check whether it is up and running to serve your request. For that go to command prompt and "ping it" to see whether it returns anything or not.
Like,
Java Code:ping some_address
If it returns a valid response, then you are good to go. If it doesn't, then you need to make sure that it's running before you try to send mail from it.
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-20-2011, 08:43 AM #10
Sometime I use JES (Java Email Server) for testing email app. You can try too.
but Goldest correctly pointed out the problem. You have mistake in configuration email.Skype: petrarsentev
http://TrackStudio.com
- 01-20-2011, 09:33 AM #11
Member
- Join Date
- Nov 2010
- Posts
- 33
- Rep Power
- 0
what if i changed my coding to: this is @ Email class.
public class Email {
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.email.msn.com");
// create some properties and get the default Session
Session session = Session.getDefaultInstance(props, null);
session.setDebug(debug);
// 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[i] = new InternetAddress(recipients);
}
msg.setRecipients(Message.RecipientType.TO, addressTo);
// Optional : You can also set your custom headers in the Email if you Want
msg.addHeader("MyHeaderName", "myHeaderValue");
// Setting the Subject and Content Type
msg.setSubject(subject);
msg.setContent(message, "text/plain");
Transport.send(msg);
}
this is my calling method:
String subject = "Chit Chit recovery";
String message = "Your username is " + u1.getUserName() + "Your password is "
+ u1.getPwd()+ ". Thank you for using Chit Chit";
String from = "guoxiang@gmail.com";
Email e1 = new Email();
e1.postMail(email, subject, message, from);
cuz right the other code, i still have the error.
- 01-20-2011, 09:51 AM #12
See, now here you are providing the details for SMTP host. But is that configured for you? Or is it a free SMTP server?
Here is a list of free SMTP servers : Free SMTP Servers. See if any of them can work for you.
But before finalizing any server, make sure that you should "ping" to it first and make sure that you are getting the response.
Hope that helps,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-20-2011, 09:57 AM #13
And please use Code Tags from next time, whenever you post your code.
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-20-2011, 10:50 AM #14
Member
- Join Date
- Nov 2010
- Posts
- 33
- Rep Power
- 0
i go to command promt and type in. ping guoxiang@gmail.com
they methion cannot find host. what does dat mean? that is a valid mail.
- 01-20-2011, 11:04 AM #15
Java Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-20-2011, 11:26 AM #16
Member
- Join Date
- Nov 2010
- Posts
- 33
- Rep Power
- 0
it does ping back. ok so now i do i send email?thanks thanks so i change my sender's address to smtp.gmail.com?
- 01-20-2011, 11:40 AM #17
It is topic about configuration gmail
send SMTP mail using JavaMail with gmail accountSkype: petrarsentev
http://TrackStudio.com
- 01-20-2011, 02:19 PM #18
No No No... Why do you want to change the sender's address?
This is SMTP host that we are talking about. You are supposed to put this in the properties as the value of your "mail.smtp.host" key. Like,
Java Code:props.put("mail.smtp.host", "smtp.gmail.com");
I hope you are clear on that,
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
- 01-20-2011, 03:48 PM #19
Member
- Join Date
- Nov 2010
- Posts
- 33
- Rep Power
- 0
Properties props = new Properties();
props.put("mail.smtp.host", "smtp.gmail.com");
i have this is my Email class.i tried send. but it still have error:
com.sun.mail.smtp.SMTPSendFailedException: 530 5.7.0 Must issue a STARTTLS command first. i16sm6986647ibl.0
what does dis means? and should i do? thanks goldest, you have been an great help.
- 01-21-2011, 07:33 AM #20
This is a weird error regarding STARTTLS and may need some domain/account settings to enable SSL.
Try a new approach, you can add some more properties to your code to get the things working. You can have,
For host and port details.Java Code:props.put("mail.smtp.host", "smtp.gmail.com"); props.put("mail.smtp.port", "465");
For authentication that you would need to do by using your gmail account username/password.Java Code:props.put("mail.smtp.auth", "true");
To see the debug activities about your send process.Java Code:props.put("mail.debug", "true");
These are the additional SSL socket factory settings.Java Code:props.put("mail.smtp.socketFactory.port", "465"); props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory"); props.put("mail.smtp.socketFactory.fallback", "false");
You would have to override the Authenticator's getPasswordAuthentication() method in order to validate the authentication.
Try out these things and see what happens?
GoldestJava Is A Funny Language... Really!.gif)
Click on * and add to member reputation, if you find their advices/solutions effective.
Similar Threads
-
how to send email notification using java
By tedy2808 in forum New To JavaReplies: 3Last Post: 08-17-2010, 01:15 PM -
simple send email APP, but when press send button appeared:
By lse123 in forum Advanced JavaReplies: 10Last Post: 06-06-2010, 06:49 PM -
how to send an email
By painamrata in forum JavaServer Pages (JSP) and JSTLReplies: 2Last Post: 02-22-2009, 04:28 PM -
send email using JMS
By Heather in forum Advanced JavaReplies: 9Last Post: 01-07-2009, 03:04 PM -
send email using apache commons email
By jnamendi in forum JavaServer Faces (JSF)Replies: 0Last Post: 10-14-2008, 05:55 PM


LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks