请参阅以下代码
我们有一个smtp服务器。
它工作正常,但经过几次测试后就已经是这样了。
我可以ping smtp服务器,它还活着。
'
package ph.com.autoemailer.util;
import javax.activation.DataHandler;
import javax.activation.DataSource;
import javax.activation.FileDataSource;
import javax.imageio.ImageIO;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;
import org.apache.commons.io.IOUtils;
import java.awt.image.BufferedImage;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URL;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
public class Mailer{
String d_email = "<this is not empty>",
d_password = "<this is not empty>",
d_host = "<this is not empty>",
d_port = "<this is not empty>",
m_to = "<this is not empty>",
//m_to = "<this is not empty>",
m_subject = "Auto-Email Notification: Receipt of Payment Confirmation FAO",
m_text = "Hey, this is the testing email.";
String formatMessage = "";
String dearGreetings;
String sbuGreetings;
String part1Message;
String part2Message;
String part2MessageImg;
String tyMessage;
String sbuMessage;
String noteMessage;
String iNoteMessage;
@SuppressWarnings("resource")
public Mailer(){
System.out.println("Start Mailer ...... ");
Properties props = new Properties();
props.put("mail.smtp.user", d_email);
props.put("mail.smtp.host", d_host);
props.put("mail.smtp.port", d_port);
props.put("mail.smtp.starttls.enable","true");
props.put("mail.smtp.auth", "true");
//props.put("mail.smtp.debug", "true");
props.put("mail.smtp.socketFactory.port", d_port);
props.put("mail.smtp.socketFactory.class", "javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback", "false");
SecurityManager security = System.getSecurityManager();
try {
Authenticator auth = new SMTPAuthenticator();
Session session = Session.getInstance(props, auth);
session.setDebug(true);
MimeMessage msg = new MimeMessage(session);
Multipart multipart = new MimeMultipart();
MimeBodyPart attachPart = new MimeBodyPart();
MimeBodyPart messageBodyPart = new MimeBodyPart();
dearGreetings = "<this is not empty>";
sbuGreetings = "<this is not empty>";
part1Message = "<this is not empty>";
part2Message = "<this is not empty>";
part2MessageImg = "<this is not empty>";;
tyMessage = "Yours truly,<br/><br/><br/>";
sbuMessage = "<this is not empty>";
noteMessage = "<this is not empty>";
iNoteMessage = "<this is not empty>";
formatMessage = dearGreetings+sbuGreetings+part1Message+part2Message+part2MessageImg+tyMessage+sbuMessage+noteMessage+iNoteMessage;
m_text = formatMessage;
msg.setText(m_text);
msg.setSubject(m_subject);
msg.setFrom(new InternetAddress(d_email));
msg.addRecipient(Message.RecipientType.TO,
new InternetAddress(m_to));
multipart.addBodyPart(messageBodyPart);
multipart.addBodyPart(attachPart);
msg.setContent(multipart);
// creates body part for the attachment
messageBodyPart.setContent(formatMessage, "text/html");
InputStream inputStream = null;
OutputStream outputStream = null;
String attachFile = "<this is not empty>";
// read this file into InputStream
inputStream = new FileInputStream(attachFile);
byte[] bytesInputStream = IOUtils.toByteArray(inputStream);
Date getDateToday = new Date();
SimpleDateFormat formatDateToday = new SimpleDateFormat("yyyyMMdd");
String finishFormatDate = formatDateToday.format(getDateToday);
// write the inputStream to a FileOutputStream
outputStream = new FileOutputStream(new File("<this is not empty>"+".pdf"));
String convertedattachFile = "<this is not empty>"+".pdf";
DataSource source = new ByteArrayDataSource(bytesInputStream, "application/pdf");//new FileDataSource(convertedattachFile);
attachPart.setDataHandler(new DataHandler(source));
attachPart.setFileName(new File(attachFile).getName());
Transport.send(msg);
} catch (Exception mex) {
mex.printStackTrace();
}finally{
System.out.println("Email Is Sending...");
}
}
private class SMTPAuthenticator extends javax.mail.Authenticator {
public PasswordAuthentication getPasswordAuthentication() {
return new PasswordAuthentication(d_password, d_password);
}
}
public static void main(String[] args) {
Mailer blah = new Mailer();
}
}
这是错误结果
Start Mailer ......
DEBUG: setDebug: JavaMail version 1.5.3
DEBUG: getProvider() returning javax.mail.Provider[TRANSPORT,smtp,com.sun.mail.smtp.SMTPTransport,Oracle]
DEBUG SMTP: need username and password for authentication
DEBUG SMTP: useEhlo true, useAuth true
DEBUG SMTP: trying to connect to host "smtp.elasticemail.com", port 2525, isSSL false
220 smtp.elasticemail.com ESMTP
DEBUG SMTP: connected to host "smtp.elasticemail.com", port: 2525
EHLO 10.24.51.120
250-smtp.elasticemail.com
250-PIPELINING
250-SIZE 20971520
250-8BITMIME
250-AUTH=PLAIN LOGIN CRAM-MD5
250-AUTH PLAIN LOGIN CRAM-MD5
250-STARTTLS
250 OK
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "20971520"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN CRAM-MD5"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN CRAM-MD5"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "OK", arg ""
STARTTLS
220 Begin TLS negotiation
EHLO 10.24.51.120
250-smtp.elasticemail.com
250-PIPELINING
250-SIZE 20971520
250-8BITMIME
250-AUTH=PLAIN LOGIN CRAM-MD5
250-AUTH PLAIN LOGIN CRAM-MD5
250-STARTTLS
250 OK
DEBUG SMTP: Found extension "PIPELINING", arg ""
DEBUG SMTP: Found extension "SIZE", arg "20971520"
DEBUG SMTP: Found extension "8BITMIME", arg ""
DEBUG SMTP: Found extension "AUTH=PLAIN", arg "LOGIN CRAM-MD5"
DEBUG SMTP: Found extension "AUTH", arg "PLAIN LOGIN CRAM-MD5"
DEBUG SMTP: Found extension "STARTTLS", arg ""
DEBUG SMTP: Found extension "OK", arg ""
DEBUG SMTP: Attempt to authenticate using mechanisms: LOGIN PLAIN DIGEST-MD5 NTLM
DEBUG SMTP: AUTH LOGIN command trace suppressed
DEBUG SMTP: AUTH LOGIN failed
javax.mail.AuthenticationFailedException: 535 Authentication failed: Error: Not enough credit.
Email Is Sending...
at com.sun.mail.smtp.SMTPTransport$Authenticator.authenticate(SMTPTransport.java:892)
at com.sun.mail.smtp.SMTPTransport.authenticate(SMTPTransport.java:814)
at com.sun.mail.smtp.SMTPTransport.protocolConnect(SMTPTransport.java:728)
at javax.mail.Service.connect(Service.java:386)
at javax.mail.Service.connect(Service.java:245)
at javax.mail.Service.connect(Service.java:194)
at javax.mail.Transport.send0(Transport.java:253)
at javax.mail.Transport.send(Transport.java:124)
at ph.com.autoemailer.util.Mailer.<init>(Mailer.java:140)
at ph.com.autoemailer.util.Mailer.main(Mailer.java:173)
答案 0 :(得分:0)
此错误是自定义错误。可能 elasticemail 服务是付费服务,您没有足够的信用来发送电子邮件。
检查您的信用。