gpt4 book ai didi

java - 如何使用java代码发送电子邮件?

转载 作者:行者123 更新时间:2023-12-01 23:01:56 25 4
gpt4 key购买 nike

嗨,我正在尝试通过java代码发送电子邮件,我已经安装了cmail服务器来发送电子邮件,但我无法发送电子邮件,我该如何发送电子邮件

这是我的代码

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

public class SendEmail
{
public static void main(String [] args){
String to = "shaktisharma27789@gmail.com";//change accordingly
String from = "admin@shakti-pc.com";
String host = "localhost";//or IP address

//Get the session object
Properties properties = System.getProperties();
properties.setProperty("mail.smtp.host", host);
Session session = Session.getDefaultInstance(properties);

//compose the message
try{
MimeMessage message = new MimeMessage(session);
message.setFrom(new InternetAddress(from));
message.addRecipient(Message.RecipientType.TO,new InternetAddress(to));
message.setSubject("Ping");
message.setText("Hello, this is example of sending email ");

// Send message
Transport.send(message);
System.out.println("message sent successfully....");

}catch (MessagingException mex) {mex.printStackTrace();}
}
}

当我运行我的程序时,我收到以下异常

com.sun.mail.smtp.SMTPSendFailedException: 550 admin@shakti-pc.com is not authorized.(WRONG SENDER MAILADDR)
;
nested exception is:
com.sun.mail.smtp.SMTPSenderFailedException: 550 admin@shakti-pc.com is not authorized.(WRONG SENDER MAILADDR)

at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.issueSendCommand(SMTPTransport.java:2108)
at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1609)
at com.sun.mail.smtp.SMTPTransport.sendMessage(SMTPTransport.java:1117)
at javax.mail.Transport.send0(Transport.java:195)
at javax.mail.Transport.send(Transport.java:124)
at SendEmail.main(SendEmail.java:27)
Caused by: com.sun.mail.smtp.SMTPSenderFailedException: 550 admin@shakti-pc.com is not authorized.(WRONG SENDER MAILADDR)

at com.sun.mail.smtp.SMTPTransport.mailFrom(SMTPTransport.java:1616)
... 4 more

我怎样才能实现我的输出?

提前致谢

最佳答案

您尚未在其中输入密码字段。此外,您还没有指定您的主机。如果您从本地主机发送电子邮件,则应该指定它。此外,如果您通过 gmail 服务器发送邮件,则应使用“smtp.gmail.com”。检查http://www.tutorialspoint.com/servlets/servlets-sending-email.htm为了澄清你的问题。通过本教程,您也可以发送带有附件的电子邮件。如果您需要jsp代码,我可以为您提供。

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE html>
<html>
<head>
<title>Simple Mail Program</title>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
</head>
<body>
<%@page import="java.sql.*"%>
<%@page import="javax.mail.*"%>

<%@page import="javax.mail.internet.*"%>
<%@ page import="java.io.*"%>
<%@ page import="java.sql.*"%>
<%@page import="java.util.*"%>
<%@ page import="java.math.BigInteger"%>



<%
String host = "smtp.gmail.com";
//host = smtp_server; //"smtp.gmail.com"; user = jsp_email; //"YourEmailId@gmail.com" // email id to send the emails
//pass = jsp_email_pw; //Your gmail password
String SSL_FACTORY = "javax.net.ssl.SSLSocketFactory";
String to_add = request.getParameter("receiver");
String subject =request.getParameter("subject");
String messageText =request.getParameter("body");
String password = request.getParameter("pwd");
String from =request.getParameter("email_id");
boolean sessionDebug = true;
Properties props = System.getProperties();
props.put("mail.host", host);
props.put("mail.transport.protocol.", "smtp");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.", "true");
props.put("mail.smtp.port", "465");
props.put("mail.smtp.socketFactory.fallback", "false");
props.put("mail.smtp.socketFactory.class", SSL_FACTORY);
Session mailSession = Session.getDefaultInstance(props, null);
mailSession.setDebug(sessionDebug);
Message msg = new MimeMessage(mailSession);
msg.setFrom(new InternetAddress(from));
InternetAddress[] address = { new InternetAddress(to_add) };
msg.setRecipients(Message.RecipientType.TO, address);
msg.setSubject(subject);
msg.setContent(messageText, "text/html"); // use setText if you want to send text
Transport transport = mailSession.getTransport("smtp");
System.setProperty("javax.net.ssl.trustStore", "conf/jssecacerts");
System.setProperty("javax.net.ssl.trustStorePassword", "admin");
transport.connect(host, from, password);
try
{
transport.sendMessage(msg, msg.getAllRecipients());
out.println("sent");
//WasEmailSent = true; // assume it was sent
}
catch (Exception err)
{
//WasEmailSent = false; // assume it's a fail
out.println("Error" + err.getMessage());
}
transport.close();
%>
</body>
</html>

关于java - 如何使用java代码发送电子邮件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23286244/

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