How do I send an e-mail in Java?

后端 未结 11 1577
一整个雨季
一整个雨季 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:36

    import java.util.Properties;
    
    import javax.mail.Authenticator;
    import javax.mail.Message;
    import javax.mail.PasswordAuthentication;
    import javax.mail.Session;
    import javax.mail.Transport;
    import javax.mail.internet.InternetAddress;
    import javax.mail.internet.MimeMessage;
    
    
    
    public class sendMail {
    
        static  String  alertByEmail(String emailMessage){
            try{
                         
                final String fromEmail = "abc@gmail.com";
                final String password = "********"; //fromEmail password 
                final String toEmail = "xyz@gmail.com";
                System.out.println("Email configuration code start");
                Properties props = new Properties();
                props.put("mail.smtp.host", "smtp.gmail.com"); //SMTP Host set by default this
                props.put("mail.smtp.port", "587"); //TLS Port you can use 465 insted of 587
                props.put("mail.smtp.auth", "true"); //enable authentication
                props.put("mail.smtp.starttls.enable", "true"); //enable STARTTLS
                //create Authenticator object to pass in Session.getInstance argument
                Authenticator auth = new Authenticator() 
                {
                
                    protected PasswordAuthentication getPasswordAuthentication() 
                    {
                            return new PasswordAuthentication(fromEmail, password);
                    }
                };
                            Session session = Session.getInstance(props, auth);
                
                            MimeMessage message = new MimeMessage(session);
                            message.setFrom(new InternetAddress(fromEmail));
                            message.addRecipient(Message.RecipientType.TO, new 
                                                                  InternetAddress(toEmail));
                            message.setSubject("ALERT");
                            message.setText(emailMessage);//here you can write a msg what you want to send... just remove String parameter in alertByEmail method oherwise call parameter
                            System.out.println("text:"+emailMessage);
                            Transport.send(message);//here mail sending process start.
                            System.out.println("Mail Sent Successfully");
            }
            catch(Exception ex)
            {
                            System.out.println("Mail fail");
                            System.out.println(ex);
            }
            return emailMessage;
            
            }
    public static void main(String[] args) {
        String emailMessage = "This mail is send using java code.Report as a spam";
        alertByEmail(emailMessage);
        }
    }
    
    
    
    
    
    
    
    
    
    
    
    https://github.com/sumitfadale/java-important-codes/blob/main/Send%20a%20mail%20through%20java 
    
    
    enter code here
    

提交回复
热议问题