简体   繁体   English

如何使用Javamail发送简单的电子邮件?

[英]How to send simple Email using Javamail?

i am creating a simple java mail program,the program is working ok and the last system print also working .but the problem is i dint received the mail in outlook.here i am using the company outlook.please some one help me. 我正在创建一个简单的Java邮件程序,该程序可以正常运行,并且最后的系统打印也可以工作。但是问题是我在Outlook.dint中收到了邮件。在这里,我正在使用公司Outlook。请提供一些帮助。 i am attaching my code here 我在这里附上我的代码

enter code here

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




public class SimpleSendEmail 
{
    public static void main(String[] args) 

    {

        String host = "compny host";
        String from = "mail id";
        String to = "usr@some.com";
        String subject = "birthday mail";
        String messageText = "I am sending a message using the"
                + " simple.\n" + "happy birthday.";
        boolean sessionDebug = false;
        Properties props = System.getProperties();
        props.put("compny host", host);
        props.put("mail.smtp.port", "25");
        props.put("mail.transport.protocol", "smtp");
        props.put("mail.smtp.starttls.enable", "true");
        Session session = Session.getDefaultInstance(props, null);
        // Set debug on the Session so we can see what is going on
        // Passing false will not echo debug info, and passing true
        // will.
        session.setDebug(sessionDebug);
        try 
        {
            Message msg = new MimeMessage(session);
            msg.setFrom(new InternetAddress(from));
            InternetAddress[] address = { new InternetAddress(to) };
            msg.setRecipients(Message.RecipientType.TO, address);
            msg.setSubject(subject);
            msg.setSentDate(new Date());
            msg.setText(messageText);

            Transport.send(msg);
            System.out.println("Sent message successfully....");
        } 

        catch (MessagingException mex)
        {
            mex.printStackTrace();
        }
    }
}



output
Sent message successfully....

"Compny host" doesn't seem like correct host. “ Compny host”似乎不是正确的主机。 Check out this tutorial http://www.tutorialspoint.com/java/java_sending_email.htm and here you have also a few examples of sending emails in Java Send email using java 查阅本教程http://www.tutorialspoint.com/java/java_sending_email.htm ,这里还有一些使用 Java 发送电子邮件的示例使用 Java 发送电子邮件

I do expect that you are using the correct host on your side. 我确实希望您使用的主机正确。

But you are missing Username and Password. 但是您缺少用户名和密码。

transport = session.getTransport("smtp");
transport.connect(hostName, port, user, password);
transport.sendMessage(message, message.getAllRecipients());

or you can use the Authenticator: 或者您可以使用身份验证器:

Session session = Session.getInstance(props,
      new javax.mail.Authenticator() {
        protected PasswordAuthentication getPasswordAuthentication() {
            return new PasswordAuthentication(username, password);
        }
      });

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM