How do I send an e-mail in Java?

后端 未结 11 1553
一整个雨季
一整个雨季 2020-12-01 01:55

I need to send e-mails from a servlet running within Tomcat. I\'ll always send to the same recipient with the same subject, but with different contents.

What\'s a sim

11条回答
  •  轻奢々
    轻奢々 (楼主)
    2020-12-01 02:50

    Tested Code send Mail with attachment :

      public class SendMailNotificationWithAttachment {
    
                public static void mailToSendWithAttachment(String messageTosend, String snapShotFile) {
    
                    String to = Constants.MailTo;
                    String from = Constants.MailFrom;
                    String host = Constants.smtpHost;// or IP address
                    String subject = Constants.subject;
                    // Get the session object
                    Properties properties = System.getProperties();
                    properties.setProperty("mail.smtp.host", host);
                    Session session = Session.getDefaultInstance(properties);
                    try {
                        MimeMessage message = new MimeMessage(session);
                        message.setFrom(new InternetAddress(from));
                        message.addRecipients(Message.RecipientType.TO, InternetAddress.parse(to));
                        message.setSubject(subject);
                        // message.setText(messageTosend);
                        BodyPart messageBodyPart = new MimeBodyPart();
                        messageBodyPart.setText(messageTosend);
                        Multipart multipart = new MimeMultipart();
                        multipart.addBodyPart(messageBodyPart);
    
                        messageBodyPart = new MimeBodyPart();
                        String filepath = snapShotFile;
    
                        DataSource source = new FileDataSource(filepath);
                        messageBodyPart.setDataHandler(new DataHandler(source));
    
                        Path p = Paths.get(filepath);
                        String NameOffile = p.getFileName().toString();
    
                        messageBodyPart.setFileName(NameOffile);
                        multipart.addBodyPart(messageBodyPart);
    
                        // Send the complete message parts
                        message.setContent(multipart);
    
                        // Send message
                        Transport.send(message);
    
            //          Log.info("Message is sent Successfully");
            //          System.out.println("Message is sent Successfully");
                        System.out.println("Message is sent Successfully");
        } catch (MessagingException e) {
        //          Log.error("Mail sending is Failed " + "due to" + e);
                    SendMailNotificationWithAttachment smnwa = new SendMailNotificationWithAttachment();
                    smnwa.mailSendFailed(e);
                    throw new RuntimeException(e);
                }
            }
    public void mailSendFailed(MessagingException e) {
            System.out.println("Mail sending is Failed " + "due to" + e);
            Log log = new Log();
            log.writeIntoLog("Mail sending is Failed " + "due to" + e.toString(), false);
        }
    
    }
    

提交回复
热议问题