从java smtpapi发送邮件

发布于 2024-11-24 17:51:51 字数 2158 浏览 1 评论 0原文

我正在尝试使用 Java 程序和 JavaMailApi 发送邮件。我已经编写了这个程序并有一个本地 SMTP 服务器。这不是问题。我不知道在主机地址中应该输入什么。请查看我的代码并让我知道我应该做什么?

    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;

// Send a simple, single part, text/plain e-mail
    public class TestEmail {

        public static void main(String[] args) {

        // SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
            String to = "[email protected]";
            String from = "[email protected]";
        // SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
            String host = "smtp.yourisp.net";

        // Create properties, get Session
            Properties props = new Properties();

        // If using static Transport.send(),
        // need to specify which host to send it to
            props.put("mail.smtp.host", host);
        // To see what is going on behind the scene
            props.put("mail.debug", "true");
            Session session = Session.getInstance(props);

            try {
            // Instantiatee a message
                Message msg = new MimeMessage(session);

            //Set message attributes
                msg.setFrom(new InternetAddress(from));
                InternetAddress[] address = {new InternetAddress(to)};
                msg.setRecipients(Message.RecipientType.TO, address);
                msg.setSubject("Test E-Mail through Java");
                msg.setSentDate(new Date());

            // Set message content
                msg.setText("This is a test of sending a " +
                        "plain text e-mail through Java.\n" +
                        "Here is line 2.");

            //Send the message
                Transport.send(msg);
        }
            catch (MessagingException mex) {
            // Prints all nested (chained) exceptions as well
                mex.printStackTrace();
        }
    }
}//End of class

I am trying to send a mail using a Java program and JavaMailApi. I have written this program and having a local SMTPServer. This is not a problem. I don't know what to put in host address. Please have a look at my code and let me know what should I do?

    import java.util.*;
    import javax.mail.*;
    import javax.mail.internet.*;
    import javax.activation.*;

// Send a simple, single part, text/plain e-mail
    public class TestEmail {

        public static void main(String[] args) {

        // SUBSTITUTE YOUR EMAIL ADDRESSES HERE!!!
            String to = "[email protected]";
            String from = "[email protected]";
        // SUBSTITUTE YOUR ISP'S MAIL SERVER HERE!!!
            String host = "smtp.yourisp.net";

        // Create properties, get Session
            Properties props = new Properties();

        // If using static Transport.send(),
        // need to specify which host to send it to
            props.put("mail.smtp.host", host);
        // To see what is going on behind the scene
            props.put("mail.debug", "true");
            Session session = Session.getInstance(props);

            try {
            // Instantiatee a message
                Message msg = new MimeMessage(session);

            //Set message attributes
                msg.setFrom(new InternetAddress(from));
                InternetAddress[] address = {new InternetAddress(to)};
                msg.setRecipients(Message.RecipientType.TO, address);
                msg.setSubject("Test E-Mail through Java");
                msg.setSentDate(new Date());

            // Set message content
                msg.setText("This is a test of sending a " +
                        "plain text e-mail through Java.\n" +
                        "Here is line 2.");

            //Send the message
                Transport.send(msg);
        }
            catch (MessagingException mex) {
            // Prints all nested (chained) exceptions as well
                mex.printStackTrace();
        }
    }
}//End of class

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

隔岸观火 2024-12-01 17:51:51

我不知道在主机地址中输入什么。

主机地址是您的 SMTP 服务器的 IP 地址或域名。

I dont know what to put in host address.

Host address is the your SMTP server's IP address or domain name.

最后的乘客 2024-12-01 17:51:51

Host是

smtp.gmail.com

gmail的,

 props.put("mail.smtp.host", "smtp.gmail.com");

应该可以用。
但请记住,要使这些邮件 api 正常工作,您应该从您的 Gmail 帐户启用 pop3 和 smtp。并且无需额外设置(例如 2 路安全登录可防止任何人使用来自其他 api 的 smtp/pop3)。

Host is

smtp.gmail.com

for gmail

 props.put("mail.smtp.host", "smtp.gmail.com");

it should work .
But remember for these mailing api to work, you should enable pop3 and smtp from your gmail account. And no extra settings ( like 2-way security login prevents any one to use smtp/pop3 from other apis ).

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文