Friday, May 11, 2012
Send Mail with Java
This is a sample code for writing with java to send email:
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
public class test {
public static void main(String args[]) throws Exception {
String user = "test";
String password = "test";
String fromAddress = "test@localhost";
String toAddress = "test@gmail.com";
Properties properties = new Properties();
properties.put("mail.smtp.host", "localhost");
properties.put("mail.smtp.port", "25");
properties.put("mail.smtp.username", user);
properties.put("mail.smtp.password", password);
properties.put("mail.transport.protocol", "smtp");
Session session = Session.getDefaultInstance(properties, null);
try
{
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(fromAddress));
message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(toAddress));
message.setSubject("Email from our JAMEs");
message.setText("hiiiiii!!");
Transport.send(message);
System.out.println("Email sent");
}
catch (MessagingException e)
{
e.printStackTrace();
}
}
}
Solution:
If we test it and it cannot send. This is the guide for resolving problem as below:
1.Go to the path : \apps\james\conf
2.Open for edit the filename 'james-fetchmail.xml'
3.Change "fetchmail" from false to true as:
4.Restart the Apache James Server.
5.You would see the following messages printed on the console after you start the server:
James Mail Server 2.3.2 Remote Manager Service started plain:4555 POP3 Service started
plain:110 SMTP Service started plain:25 NNTP Service started plain:119 FetchMail Started.
Labels:
Java
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment