import java.security.Security;
import java.util.Properties;
import javax.mail.Message;
import javax.mail.NoSuchProviderException;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.PasswordAuthentication;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;
public class SendMail {
public String to;
public String subject;
public String text;
SendMail(String to, String subject, String text){
this.to = to;
this.subject = subject;
this.text = text;
}
public void send() throws NoSuchProviderException, AddressException{
try
{
Security.addProvider(new com.sun.net.ssl.internal.ssl.Provider());
Properties props=new Properties();
props.setProperty("mail.transport.protocol","smtp");
props.setProperty("mail.host","mail.epro-tech.com");
props.put("mail.smtp.auth","true");
props.put("mail.smtp.port","465");
props.put("mail.debug","true");
props.put("mail.smtp.socketFactory.port","465");
props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.socketFactory.fallback","false");
Session session=Session.getDefaultInstance(props,new GJMailAuthenticator());
session.setDebug(true);
Transport transport=session.getTransport();
InternetAddress addressFrom=new InternetAddress("itopstest@epro-tech.com");
MimeMessage message=new MimeMessage(session);
message.setSender(addressFrom);
message.setSubject(subject);
message.setContent(text,"text/html");
InternetAddress addressTo=new InternetAddress(to);
message.setRecipient(Message.RecipientType.TO,addressTo);
transport.connect();
Transport.send(message);
transport.close();
System.out.println("DONE");
}
catch(Exception e)
{
e.printStackTrace();
}
}
}
class GJMailAuthenticator extends javax.mail.Authenticator{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("itopstest@epro-tech.com","Ops@890T");
}
}
public class Mail extends SendMail {
public static void main(String[] args) {
String to = "noreply@eprocorp.com";
String subject = "Test";
String message = "A test message";
SendMail SendMail = new SendMail(to, subject, message);
try
{
SendMail.send();
}
catch (Exception e)
{
//
}
}
}
收到错误
Mail.java:18: error: cannot find symbol
SendMail SendMail = new SendMail(to, subject, message);
^
symbol: class SendMail
location: class Mail
Mail.java:18: error: cannot find symbol
SendMail SendMail = new SendMail(to, subject, message);
^
symbol: class SendMail
location: class Mail
2 errors
任何人都可以建议我如何纠正这个