package com.mail;import java.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SimpleMailSendMessage{
public static void main(String [] args){
String host ="163.com";
String to = "[email protected]";
String from = "[email protected]";
String subject = "javaMailTest";
String messageText = "Hello ,java mail";
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.host",host);
props.put("mail.transport.protocol","smtp");
Session session = Session.getDefaultInstance(props,null);
session.setDebug(sessionDebug);
try{
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress [] address = {new InternetAddress(to)};
msg.setRecipients(javax.mail.Message.RecipientType.To,address);//找不到符号
//只有javax.mail.Message.RecipientType 没有这个TO
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
Transport.send(msg);
}catch(Exception e){
e.printStackTrace();
}
}
}大家帮帮我,来指导下
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SimpleMailSendMessage{
public static void main(String [] args){
String host ="163.com";
String to = "[email protected]";
String from = "[email protected]";
String subject = "javaMailTest";
String messageText = "Hello ,java mail";
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.host",host);
props.put("mail.transport.protocol","smtp");
Session session = Session.getDefaultInstance(props,null);
session.setDebug(sessionDebug);
try{
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress [] address = {new InternetAddress(to)};
msg.setRecipients(javax.mail.Message.RecipientType.To,address);//找不到符号
//只有javax.mail.Message.RecipientType 没有这个TO
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
Transport.send(msg);
}catch(Exception e){
e.printStackTrace();
}
}
}大家帮帮我,来指导下
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;
public class SimpleMailSendMessage{
public static void main(String [] args){
String host ="server.163.com";
String to = "[email protected]";
String from = "[email protected]";
String subject = "javaMailTest";
String messageText = "Hello ,java mail";
boolean sessionDebug = false;
Properties props = System.getProperties();
props.put("mail.smtp.host",host);
props.put("mail.transport.protocol","smtp");
props.put("mail.smtp.port",25);
Session session = Session.getDefaultInstance(props,null);
session.setDebug(sessionDebug);
try{
Message msg=new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress [] address = {new InternetAddress(to)};
msg.setRecipients(javax.mail.Message.RecipientType.TO,address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText);
Transport.send(msg);
System.out.println("send sucess");
}catch(Exception e){
e.printStackTrace();
}
}
}这样还是不行呀,
运行时 出错,
javax.mail.MessagingException: Could not connect to SMTP host: server.163.com, p
ort: 25, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1235)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:34
0)
at javax.mail.Service.connect(Service.java:275)
at javax.mail.Service.connect(Service.java:156)
at javax.mail.Service.connect(Service.java:105)
at javax.mail.Transport.send0(Transport.java:168)
at javax.mail.Transport.send(Transport.java:98)
at com.mail.SimpleMailSendMessage.main(SimpleMailSendMessage.java:33)
Press any key to continue...
ort: 25, response: -1
at com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1235)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:34
0)
//--------------------------------------------不是server.163.com应该是smtp.163.com吧?你试试.关注..........
我对java发邮件也不是很了解,提供一个程序作参考,不对的地方还请批正。import java.util.Date;
import java.util.Properties;import javax.mail.Authenticator;
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 SimpleMailSendMessage {
public static void main(String[] args) {
String host = "smtp.163.com";
String to = "[email protected]";
String from = "[email protected]";
String subject = "javaMailTest";
String messageText = "Hello ,java mail";
boolean sessionDebug = false; Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.smtp.port", 25);
props.put("mail.transport.protocol", "smtp");
props.put("mail.smtp.auth", "true"); Session session = Session.getDefaultInstance(props,
new Authenticator() {
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("[email protected]","yourpassword");
}
}); session.setDebug(sessionDebug);
try {
Message msg = new MimeMessage(session);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to) };
msg.setRecipients(javax.mail.Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setSentDate(new Date());
msg.setText(messageText); Transport.send(msg);
} catch (Exception e) {
e.printStackTrace();
} }}