Lite sent kanske men här kommer ett svar och kod exempel
package com.vgsoftware.util.internet.mail;
import javax.mail.Session;
import javax.mail.Message;
import javax.mail.Transport;
import javax.mail.MessagingException;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import java.io.IOException;
import java.util.Properties;
/**
* A simple class that sends a email
*
* @author Viktor Gars
* @version 1.0
*/
public class Mail
{
private String host="";
/**
* Default constructor
*/
public Mail()
{
}
/**
* Constructor that takes the mail host that will be used when sending mails as a parameter
*
* @param host the SMTP mail server that will be used for sending the email.
*/
public Mail(String host)
{
this.host=host;
}
/**
* Send a email
*
* @param to The email address to the reciver.
* @param from The sendars email address.
* @param subject The subject of the email.
* @param text The body text of the email.
*
* @return true if successfull, false if somthing went wrong
*/
public boolean sendMail(String to, String from, String subject, String text)
{
return(sendMail(host, to, from, subject, text));
}
/**
* Send a email
*
* @param host the SMTP mail server that will be used for sending the email.
* @param to The email address to the reciver.
* @param from The sendars email address.
* @param subject The subject of the email.
* @param text The body text of the email.
*
* @return true if successfull, false if somthing went wrong
*/
public boolean sendMail(String host, String to, String from, String subject, String text)
{
try
{
// Get system properties
Properties props = System.getProperties();
// Setup mail server
props.put("mail.smtp.host", host);
// Get session
Session session = Session.getDefaultInstance(props, null);
// Define message
MimeMessage message = new MimeMessage(session);
// Set the from address
message.setFrom(new InternetAddress(from));
// Set the to address
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
// Set the subject
message.setSubject(subject);
// Set the content
message.setText(text);
// Send message
Transport.send(message);
}
catch(AddressException e)
{
e.printStackTrace(System.err);
return(false);
}
catch(MessagingException e)
{
e.printStackTrace(System.err);
return(false);
}
return(true);
}
}
/Viktor