Total members 11894 |It is currently Thu Nov 21, 2024 8:28 pm Login / Join Codemiles

Java

C/C++

PHP

C#

HTML

CSS

ASP

Javascript

JQuery

AJAX

XSD

Python

Matlab

R Scripts

Weka





java code
public void sendMail(Mail mail) {

Message msg = new MimeMessage(session);



try {
if (mail.getSender() != null) {
msg.setFrom(toAddress(mail.getSender()));
}
} catch (MessagingException messagingException) {
// Do something
}

try {
msg.setRecipients(Message.RecipientType.TO, toAddresses(mail
.getToRecipient()));
} catch (MessagingException messagingException) {

// Do something
}

try {
msg.setRecipients(Message.RecipientType.CC, toAddresses(mail
.getCcRecipient()));
} catch (MessagingException messagingException) {
// Do something
}

try {
msg.setRecipients(Message.RecipientType.BCC, toAddresses(mail
.getBccRecipient()));
} catch (MessagingException messagingException) {
// Do something
}

Multipart messageParts = new MimeMultipart();
MimeBodyPart textPart = new MimeBodyPart();

try {
msg.setSubject(mail.getSubject());
} catch (MessagingException messagingException) {
// Do something
}

try {
textPart.setText(mail.getMessage(), "UTF8");
} catch (MessagingException messagingException) {

// Do somthing
}

try {
messageParts.addBodyPart(textPart);
} catch (MessagingException messagingException) {
// Do something
}

if (mail.getAttachments() != null) {
for (int i = 0; i < mail.getAttachments().length; i++) {
File file = mail.getAttachments()[i];

if (!file.exists()) {
// Do Something
}
DataSource dataSource = new FileDataSource(file);
MimeBodyPart attachmentPart = new MimeBodyPart();

try {
attachmentPart.setDataHandler(new DataHandler(dataSource));
} catch (MessagingException messagingException) {

// Do Something
}

try {
attachmentPart.setFileName(file.getName());
} catch (MessagingException messagingException) {

// Do Something
}

try {
messageParts.addBodyPart(attachmentPart);
} catch (MessagingException messagingException) {

// Do Something
}
}
}

try {
msg.setContent(messageParts);

} catch (MessagingException messagingException) {
// Do Something

}

try {

Transport.send(msg);
} catch (MessagingException messagingException) {
// Do Something
}

}




_________________
M. S. Rakha, Ph.D.
Queen's University
Canada


Author:
Mastermind
User avatar Posts: 2715
Have thanks: 74 time
Post new topic Reply to topic  [ 1 post ] 

  Related Posts  to : Send Email Recipient CC,BCC MimeMultipart, Attachments
 Send email in asp     -  
 Send an email in asp.net     -  
 Send email link example.     -  
 send email from ASP using CDOSYS     -  
 Send Email from a PHP Script Example     -  
 send confirmation link to email     -  
 i want to send conformation email with user details on the     -  
 background attachments     -  
 email through JAVA program     -  



Topic Tags

Java Email






Powered by phpBB © 2000, 2002, 2005, 2007 phpBB Group
All copyrights reserved to codemiles.com 2007-2011
mileX v1.0 designed by codemiles team
Codemiles.com is a participant in the Amazon Services LLC Associates Program, an affiliate advertising program designed to provide a means for sites to earn advertising fees by advertising and linking to Amazon.com