Mail w/o attachments
public class SendJavaMail
{
public static void main(String[] args)
{
try
{
// Create a properties file containing the host address of
// your SMTP server
Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", "smtp.myispserver.net");
// Create a session with the Java Mail API
Session mailSession =
Session.getDefaultInstance(mailProps);
// Create a transport object for sending mail
Transport transport = mailSession.getTransport("smtp");
// Create a new mail message
MimeMessage message = new MimeMessage(mailSession);
// Set the From and the Recipient
message.setFrom(new InternetAddress(
"senorcoffeebean@wutka.com"));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress("mark@wutka.com"));
// Set the subject
message.setSubject("Hello from Java Mail!");
// Set the message text
message.setText("Hello!\n"+
"It is I, Senor Coffee Bean! I bring you greetings \n"+
"from the Java Mail API!. \n"+
" Senor Coffee Bean, Esp.\n");
// Save all the changes you have made to the message
message.saveChanges();
// Send the message
transport.send(message);
}
catch (Exception exc)
{
exc.printStackTrace();
}
}
}
public class SendJavaMail
{
public static void main(String[] args)
{
try
{
// Create a properties file containing the host address of
// your SMTP server
Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", "smtp.myispserver.net");
// Create a session with the Java Mail API
Session mailSession =
Session.getDefaultInstance(mailProps);
// Create a transport object for sending mail
Transport transport = mailSession.getTransport("smtp");
// Create a new mail message
MimeMessage message = new MimeMessage(mailSession);
// Set the From and the Recipient
message.setFrom(new InternetAddress(
"senorcoffeebean@wutka.com"));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress("mark@wutka.com"));
// Set the subject
message.setSubject("Hello from Java Mail!");
// Set the message text
message.setText("Hello!\n"+
"It is I, Senor Coffee Bean! I bring you greetings \n"+
"from the Java Mail API!. \n"+
" Senor Coffee Bean, Esp.\n");
// Save all the changes you have made to the message
message.saveChanges();
// Send the message
transport.send(message);
}
catch (Exception exc)
{
exc.printStackTrace();
}
}
}
Mail w/ attachments and text:
import java.io.*;
import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
/** Uses the Java Mail API to send a message via SMTP. */
public class SendAttachment
{
public static void main(String[] args)
{
try
{
// Create a properties file containing the host address of
// your SMTP server
Properties mailProps = new Properties();
mailProps.put("mail.smtp.host", "smtp.myispserver.net");
// Create a session with the Java Mail API
Session mailSession =
Session.getDefaultInstance(mailProps);
// Create a transport object for sending mail
Transport transport = mailSession.getTransport("smtp");
// Create a new mail message
MimeMessage message = new MimeMessage(mailSession);
// Set the From and the Recipient
message.setFrom(new InternetAddress(
"senorcoffeebean@wutka.com"));
message.setRecipient(Message.RecipientType.TO,
new InternetAddress("mark@wutka.com"));
// Set the subject
message.setSubject("Hello from Java Mail!");
// Create the multipart object for doing attachments
MimeMultipart multi = new MimeMultipart();
// Create the message part for the main message text
BodyPart textBodyPart = new MimeBodyPart();
// Set the message text
textBodyPart.setText("Hello!\n"+
"It is I, Senor Coffee Bean! I bring you an attachment \n"+
"from the Java Mail API!. \n"+
" Senor Coffee Bean, Esp.\n");
// Add the body part to the multipart object
multi.addBodyPart(textBodyPart);
// Create an input stream to read the attachment data
FileInputStream in = new FileInputStream("SendAttachment.java");
// Create the body part for the attachment
BodyPart fileBodyPart = new MimeBodyPart(in);
// Give the attachment a filename (the name that appears when someone
// reads the message--it doesn't have to be the same as the file
// you're reading).
fileBodyPart.setFileName("SendAttachment.java");
// Add the attachment to the multipart object
multi.addBodyPart(fileBodyPart);
// The multipart object is the content for the email message
message.setContent(multi);
// Save all the changes you have made to the message
message.saveChanges();
// Send the message
transport.send(message);
}
catch (Exception exc)
{
exc.printStackTrace();
}
}
}
Subscribe to:
Post Comments (Atom)
1 comment:
very nice.........
http://www.phsharma.co.cc
Post a Comment