I have to develop a program in java using java mail api in which I have to send a mail rite now i have coded the java program in fashion in which i can have multiple recipients in TO section of mail and in multiple recipients in CC section also
earlier I have coded the below java program in which attachments were also send but rite now I have decided to remove the functionality of sending attachments as attachements were not to be sent only simple mail was send to the clients
so below is the method in which parameters were passes and now please advise is the below implemtation of method is correct , as i want to remove the attachment code from it as aatachments are not send and only simple mails without attachment is need to be sent so please advise how can i remove the extra piece of code from this mail
public static void sendEmail(String mailTo[], String mailCc[], String from, String subject, String text, String smtpHost , String mailSmtpPort) throws Exception, DocumentException, IOException {
try {
Properties properties = new Properties();
properties.put("mail.smtp.host", smtpHost);
properties.put("mailSmtpPort", mailSmtpPort);
//obtaining the session
Session emailSession = Session.getDefaultInstance(properties);
//Enable for debuging
emailSession.setDebug(true);
Message emailMessage = new MimeMessage(emailSession);
InternetAddress[] addressTo = new InternetAddress[mailTo.length];
for (int i = 0; i < mailTo.length; i++) {
addressTo[i] = new InternetAddress(mailTo[i]);
}
emailMessage.setRecipients(RecipientType.TO, addressTo);
InternetAddress[] addresscc = new InternetAddress[mailCc.length];
for (int i = 0; i < mailCc.length; i++) {
addresscc[i] = new InternetAddress(mailCc[i]);
}
emailMessage.setRecipients(RecipientType.CC, addresscc);
emailMessage.setFrom(new InternetAddress(from));
emailMessage.setSubject(subject);
// Create the message part ****** is this part is required as attachemts are no more being sent ***
BodyPart messageBodyPart = new MimeBodyPart();
messageBodyPart.setContent(text, "text/html");
// messageBodyPart.setText(text);
// Create a multipart message
Multipart multipart = new MimeMultipart();
multipart.addBodyPart(messageBodyPart);
// attachment part
MimeBodyPart attachPart = new MimeBodyPart();
//String filename = "c:\\ap.xls";
//DataSource source = new FileDataSource(filename);
//attachPart.setDataHandler(new DataHandler(source));
//attachPart.setFileName(filename);
multipart.addBodyPart(attachPart);
// Send the complete message parts
emailMessage.setContent(multipart);
Transport.send(emailMessage);
} catch (AddressException e) {
e.printStackTrace();
} catch (MessagingException e) {
e.printStackTrace();
}
}
Change the declaration to:
MimeMessage emailMessage = new MimeMessage(emailSession);
Between setSubject and send, replace all the code with:
emailMessage.setText(text, null, "html");
Replace Session.getDefaultInstance with Session.getInstance .
Remove the "mailSmtpPort" property since it does nothing. (Did you just make that up, or did you copy that from someone else?)
Simple enough?
Did you know there's lots of sample code available, and more information in the JavaMail FAQ ?
注释掉multipart.addBodyPart(attachPart);
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.