gpt4 book ai didi

javax.mail.MessagingException :

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:59:00 27 4
gpt4 key购买 nike

<分区>

package com.mca2b;
import java.util.Properties;
import javax.mail.util.*;
import javax.mail.*;
import javax.mail.internet.*;
import javax.activation.*;


public class SampleMail
{
public static void main(String [] args) throws Exception{
String to = "kalgaonkarsiddhesh@gmail.com";//change accordingly
String from = "siddhesh.kalgaonkar@ves.ac.in";//change accordingly
String host = "localhost";//or IP address
// 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 (SendFailedException mex) {
mex.printStackTrace();
}

}

}

我在线程“main”javax.mail.MessagingException 中遇到异常。无法连接到 SMTP 主机:localhost,端口:25 嵌套异常是:java.net.ConnectException:连接被拒绝:连接。那么解决方案是什么?

我在这里发布了在 struts 2 中通过 gmail 发送邮件的正确代码。对于不同的提供商,您必须相应地更改 SMTP 主机:)

package org.entity;

import com.opensymphony.xwork2.ActionSupport;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

public class DonorForgotAction extends ActionSupport
{
DonorForgot forg;
public DonorForgotAction() {
// TODO Auto-generated constructor stub
}
public DonorForgot getForg() {
return forg;
}
public void setForg(DonorForgot forg) {
this.forg = forg;
}
@Override
public String execute() throws Exception {
String email,pass;
final String username = "";
final String password = "";
Properties props = new Properties();
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.starttls.enable", "true");
props.put("mail.smtp.host", "smtp.gmail.com");
props.put("mail.smtp.port", "587");

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

try {
Class.forName("com.mysql.jdbc.Driver");
Connection con = DriverManager.getConnection(
"jdbc:mysql://localhost:3306/foryou", "root", "siddheshkk");
System.out.println("Driver Loaded");
PreparedStatement ps = con
.prepareStatement("select email,password from donorinfo where contact=?");
ps.setString(1, forg.getContact());
ResultSet rs = ps.executeQuery();
if (rs.next()) {
email=rs.getString("email");
pass=rs.getString("password");
System.out.println("Here is your email id "+email+"and password is "+pass);
try {
Message message = new MimeMessage(session);
message.setFrom(new InternetAddress(username));
message.setRecipients(Message.RecipientType.TO,
InternetAddress.parse(email));
message.setSubject("Password Retrieval");
message.setText("Dear user your password is "+ pass);
Transport.send(message);
System.out.println("Done");

} catch (MessagingException e) {
e.printStackTrace();
}
return "success";
} else {
return "error";
}
} catch (Exception e) {
e.printStackTrace();
}
return "success";
}

}

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com