当前位置: 技术问答>java相关
如何在局域网中实现邮件发送?
来源: 互联网 发布时间:2015-10-04
本文导语: JSP开发邮件系统中遇到的问题,在局域网中无法连接SMTP服务器,如何实现类似 OUTLOOK EXPRESS的连接设置功能? | 我这儿有代码,供你参考(发信需要身份验证): import javax.mail.*; import javax.mail.internet.*;...
JSP开发邮件系统中遇到的问题,在局域网中无法连接SMTP服务器,如何实现类似
OUTLOOK EXPRESS的连接设置功能?
OUTLOOK EXPRESS的连接设置功能?
|
我这儿有代码,供你参考(发信需要身份验证):
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class sendMail
{
public static void main(String args[]) throws Exception
{
String host = "smtp.sina.com.cn";
String from = "javamail@sina.com";
String to = "javamail@china.com";
String username = "javamail";
String password = "password";
// Get system properties
// Properties props = System.getProperties(); 很多例子中是这样的,其实下面这句更好,可以用在applet中
Properties props = new Properties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true"); //这样才能通过验证
// Get session
Session session = Session.getDefaultInstance(props);
// watch the mail commands go by to the mail server
session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail");
// Send message
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
import javax.mail.*;
import javax.mail.internet.*;
import java.util.*;
public class sendMail
{
public static void main(String args[]) throws Exception
{
String host = "smtp.sina.com.cn";
String from = "javamail@sina.com";
String to = "javamail@china.com";
String username = "javamail";
String password = "password";
// Get system properties
// Properties props = System.getProperties(); 很多例子中是这样的,其实下面这句更好,可以用在applet中
Properties props = new Properties();
// Setup mail server
props.put("mail.smtp.host", host);
props.put("mail.smtp.auth", "true"); //这样才能通过验证
// Get session
Session session = Session.getDefaultInstance(props);
// watch the mail commands go by to the mail server
session.setDebug(true);
// Define message
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,
new InternetAddress(to));
message.setSubject("Hello JavaMail");
message.setText("Welcome to JavaMail");
// Send message
message.saveChanges();
Transport transport = session.getTransport("smtp");
transport.connect(host, username, password);
transport.sendMessage(message, message.getAllRecipients());
transport.close();
}
}
|
我遇到类试的问题
把邮件服务器的地址改为IP地址就能连接上了
比如说smtp.XXX.com是服务器的话
改成IP地址不要加SMTP
把邮件服务器的地址改为IP地址就能连接上了
比如说smtp.XXX.com是服务器的话
改成IP地址不要加SMTP