0

对于我的一个 j2ee 应用程序,我使用 SMTP 发送邮件。

我的代码如下所示。

package email_sender;

import java.util.Properties;

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 registration
{
    public void send(String name, String email)
    {
        String email_format =   "TEMP MESSAGE";

        Properties props = new Properties();
        props.put("mail.smtp.host", "smtp.gmail.com");
        props.put("mail.smtp.socketFactory.port", "465");
        props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
        props.put("mail.smtp.auth", "true");
        props.put("mail.smtp.port", "465");

        Session session = Session.getDefaultInstance(props,
            new javax.mail.Authenticator() 
            {
                protected PasswordAuthentication getPasswordAuthentication() 
                {
                    return new PasswordAuthentication("gmail@gmail.com","gmail_password");
                }
            });


        try 
        {

            Message message = new MimeMessage(session);
            message.setFrom(new InternetAddress("gmail.com"));
            message.setRecipients(Message.RecipientType.TO, InternetAddress.parse(email));
            message.setSubject("TEST subject");
            message.setContent(email_format, "text/html");
            Transport.send(message); 
        }
        catch (Exception e) 
        {
            throw new RuntimeException(e);
        }
    }
}

这是注册表的测试格式。

它需要将近 9 秒的时间来执行。

我正在使用 2MBPS 宽带连接。

是过程有问题还是我们需要找到一些更好的方式来发送邮件?

4

0 回答 0