从一台机器发送简单的电子邮件到另一台 [英] Send simple email from one machine to other

查看:88
本文介绍了从一台机器发送简单的电子邮件到另一台的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

  //  文件名SendEmail.java  

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

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

// 需要提及收件人的电子邮件ID。
字符串 to = abcd@gmail.com;

// 需要提及发件人的电子邮件ID
< span class =code-sdkkeyword> String from = xyz@gmail.com;

// 假设您要从localhost发送电子邮件
< span class =code-sdkkeyword> String host = localhost;

// 获取系统属性
属性properties = System。的GetProperties();

// 设置邮件服务器
properties.setProperty(< span class =code-string> mail.smtp.host,host);

// 获取默认的Session对象。
会话会话= Session.getDefaultInstance(properties);

尝试 {
// 创建默认的MimeMessage对象。
MimeMessage message = new MimeMessage(session);

// Set From:标题字段。
message.setFrom( new InternetAddress(from));

// 设置为:标题的标题字段。
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));

// 设置主题:标题字段
message.setSubject ( 这是主题行!);

// 现在设置实际消息
message.setText ( 这是实际消息);

// 发送消息
Transport.send(消息) ;
System.out.println( 已成功发送消息....);
} catch (MessagingException mex){
mex.printStackTrace();
}
}
}



错误发生在图片中

http://postimage.org/image/qesglw7l1/ [ ^ ]





//我的classpath指向mail.jar和activation.jar

解决方案

查看本教程:



JavaMail API的基础 [ ^ ]

所有关于JavaMail [ ^ ]



相当不错,一切都在那里。



像gmail这样的网络电子邮件帐户无法用于开发,因为它们允许您在有限的基础上要求访问,并在您登录失败时禁止您。



如果您需要电子邮件服务器,请尝试使用HMail Server: http://www.hmailserver.com/ [ ^ ](在设置中关闭自动禁止选项!)

// File Name SendEmail.java

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

public class SendEmail
{
   public static void main(String [] args)
   {
      
      // Recipient's email ID needs to be mentioned.
      String to = "abcd@gmail.com";

      // Sender's email ID needs to be mentioned
      String from = "xyz@gmail.com";

      // Assuming you are sending email from localhost
      String host = "localhost";

      // Get system properties
      Properties properties = System.getProperties();

      // Setup mail server
      properties.setProperty("mail.smtp.host", host);

      // Get the default Session object.
      Session session = Session.getDefaultInstance(properties);

      try{
         // Create a default MimeMessage object.
         MimeMessage message = new MimeMessage(session);

         // Set From: header field of the header.
         message.setFrom(new InternetAddress(from));

         // Set To: header field of the header.
         message.addRecipient(Message.RecipientType.TO,
                                  new InternetAddress(to));

         // Set Subject: header field
         message.setSubject("This is the Subject Line!");

         // Now set the actual message
         message.setText("This is actual message");

         // Send message
         Transport.send(message);
         System.out.println("Sent message successfully....");
      }catch (MessagingException mex) {
         mex.printStackTrace();
      }
   }
}


error occurring is shown in image
http://postimage.org/image/qesglw7l1/[^]


// i have classpath pointing to mail.jar and activation.jar

解决方案

check this tutorial:

Fundamentals of the JavaMail API[^]
All about JavaMail[^]

pretty good, it''s all in there.

A web email account like gmail will not work for development, as they let you demand access on a limited base and will ban you when you fail with your login.

If you need a email server try HMail Server: http://www.hmailserver.com/[^] (auto-ban option to be switched off in settings!)


这篇关于从一台机器发送简单的电子邮件到另一台的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆