我想使用Java发送邮件但是使用我在这里http://www.tutorialspoint.com/java/java_sending_email.htm找到的本教程不起作用。
这是我的主要方法
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 = "web@gmail.com";
// Assuming you are sending email from localhost
String host = "localhost";
// Get system properties
Properties properties = System.getProperties();
// Setup mail server
properties.setProperty("smtp.laposte.net", 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();
}
}
我有以下错误:
javax.mail.MessagingException:无法连接到SMTP主机:localhost,port:25; 嵌套异常是: java.net.ConnectException:连接被拒绝:连接 在com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1972) at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:642) 在javax.mail.Service.connect(Service.java:295) 在javax.mail.Service.connect(Service.java:176) 在javax.mail.Service.connect(Service.java:125) 在javax.mail.Transport.send0(Transport.java:194) 在javax.mail.Transport.send(Transport.java:124) 在javamail.JavaMail.main(JavaMail.java:58) 引起:java.net.ConnectException:连接被拒绝:连接 at java.net.DualStackPlainSocketImpl.connect0(Native Method) 在java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:79) 在java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350) at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206) 在java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188) 在java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172) 在java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392) 在java.net.Socket.connect(Socket.java:589) 在java.net.Socket.connect(Socket.java:538) 在com.sun.mail.util.SocketFetcher.createSocket(SocketFetcher.java:319) 在com.sun.mail.util.SocketFetcher.getSocket(SocketFetcher.java:233) 在com.sun.mail.smtp.SMTPTransport.openServer(SMTPTransport.java:1938) ......还有7个 建立成功(总时间:1秒)
答案 0 :(得分:1)
错误在主机使用中
host="smtp.gmail.com";
如果您使用gmail ID发送邮件。然后你需要打开gmail从extrnel应用程序发送邮件给用户。 请点击以下链接https://support.google.com/a/answer/57919?hl=en 打开然后再试一次。
答案 1 :(得分:1)
您也可以使用spring发送邮件。
bean.xml
<bean id="myMailSender" class="org.springframework.mail.javamail.JavaMailSenderImpl">
<property name="host" value="smtp.gmail.com" />
<property name="port" value="587" />
<property name="username" value="<user_name>" />
<property name="password" value="<password>" />
<!-- The name of the property, following JavaBean naming conventions -->
<property name="javaMailProperties">
<props>
<prop key="mail.transport.protocol">smtp</prop>
<prop key="mail.smtp.auth">true</prop>
<prop key="mail.smtp.starttls.enable">true</prop>
<prop key="mail.debug">true</prop>
</props>
</property>
</bean>
@Service("myMail")
public class myMailApi {
@Autowired
private MailSender myMailSender;
public void sendMail(String to,String from,String subject, String body){
SimpleMailMessage simpleMailMessage = new SimpleMailMessage();
simpleMailMessage.setTo(to);
simpleMailMessage.setFrom(from);
simpleMailMessage.setSubject(subject);
simpleMailMessage.setText(body);
myMailSender.send(simpleMailMessage);
}
}
private static void sendMailClient() {
String mailXml = "mail-bean.xml";
ApplicationContext context = new ClassPathXmlApplicationContext(mailXml);
myMailApi mailBean = (mmyMailApi) context
.getBean("myMail");
mailBean.sendMail("<to>",
"<from>", "<subject>", "<body>");
}
答案 2 :(得分:0)
我可以使用哪个服务器发送电子邮件而无需帐户并登录java mail api? hotmail是否在不登录java邮件的情况下自动发送电子邮件?