javax.mail.NoSuchProviderException: Nieprawidłowy protokół: null
Mamy aplikację gniazdową, która wysyła całkiem sporo wiadomości e-mail. Postanowiliśmy więc wysłać do niego ogromną liczbę wiadomości, która wywoła e-maile. W końcu widzimy, że e-mail zajmuje wiele godzin, zanim dotrze do skrzynki odbiorczej albo gmail, hotmail lub yahoo itp. Mamy te kody na początku.
public class commuSe {
BoneCP connectionPool = null;
class ConnectionHandler implements Runnable {
private Socket receivedSocketConn1;
ConnectionHandler(Socket receivedSocketConn1) {
this.receivedSocketConn1=receivedSocketConn1;
}
public void run() {
.....
}
void sendClientEmail(String emailMessageString)
{
try
{
Properties props = new Properties();
props.put("mail.smtp.host", "**********");
props.put("mail.smtp.socketFactory.port", "******");
//props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "*****");
Session session = Session.getDefaultInstance(props,new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("*********","*******");
}
});
int count=0;
System.out.println("\n\nClient Email queue took ready :"+emailMessageString);
try
{
String[] eMArray = null;
eMArray = emailMessageString.split("@EmL@");
Message emailMessage = new MimeMessage(session);
emailMessage.setFrom(new InternetAddress("****************"));
if(eMArray.length>1)
{
for(int iEmail=1; iEmail<eMArray.length ; iEmail++)
{
String cc1 = eMArray[iEmail];
emailMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(cc1));
}
emailMessage.setRecipients(Message.RecipientType.BCC,InternetAddress.parse("**************"));
}
else
{
emailMessage.setRecipients(Message.RecipientType.TO,InternetAddress.parse("*************"));
}
emailMessage.setSubject("Alerts");
emailMessage.setText(eMArray[0]);
Transport.send(emailMessage);
}
catch (Exception e)
{
System.out.println("Transport Problem");
e.printStackTrace();
}
}
catch (Exception e)
{
System.out.println("Main email try got problem");
e.printStackTrace();
}
}
}
Więc na podstawie tego linkuJak skutecznie wysyłać masowe wiadomości e-mail za pomocą interfejsu API javax.mail? & Czy możemy wykorzystać ponowne sesje uwierzytelnione w celu zwiększenia szybkości? staraliśmy się to zmienić w następujący sposób. Ale skończyć z wyjątkiem poczty. Próbowaliśmy zbudować tylko jedną sesję i ponownie używać, aby uniknąć opóźnienia dostarczenia poczty. Deklarujemy to na górze Session session = null; aby zapisać utworzoną sesję?
public class commuSe {
BoneCP connectionPool = null;
Session session = null;
class ConnectionHandler implements Runnable {
private Socket receivedSocketConn1;
ConnectionHandler(Socket receivedSocketConn1) {
this.receivedSocketConn1=receivedSocketConn1;
}
public void run() {
.....
}
void sendClientEmail(String emailMessageString)
{
try
{
int count=0;
System.out.println("\n\nClient Email queue took ready :"+emailMessageString);
try
{
String[] eMArray = null;
eMArray = emailMessageString.split("@EmL@");
Message emailMessage = new MimeMessage(session);
emailMessage.setFrom(new InternetAddress("****************"));
if(eMArray.length>1)
{
for(int iEmail=1; iEmail<eMArray.length ; iEmail++)
{
String cc1 = eMArray[iEmail];
emailMessage.addRecipient(Message.RecipientType.TO,new InternetAddress(cc1));
}
emailMessage.setRecipients(Message.RecipientType.BCC,InternetAddress.parse("**************"));
}
else
{
emailMessage.setRecipients(Message.RecipientType.TO,InternetAddress.parse("*************"));
}
emailMessage.setSubject("Alerts");
emailMessage.setText(eMArray[0]);
Transport t = session.getTransport();
t.connect();
t.sendMessage(emailMessage, emailMessage.getAllRecipients()); }
catch (Exception e)
{
System.out.println("Transport Problem");
e.printStackTrace();
}
}
catch (Exception e)
{
System.out.println("Main email try got problem");
e.printStackTrace();
}
}
}
public static void main(String[] args) {
new commuSe ();
}
commuSe () {
Properties props = new Properties();
props.put("mail.smtp.host", "**********");
props.put("mail.smtp.socketFactory.port", "******");
//props.put("mail.smtp.socketFactory.class","javax.net.ssl.SSLSocketFactory");
props.put("mail.smtp.auth", "true");
props.put("mail.smtp.port", "*****");
session = Session.getInstance(props,new javax.mail.Authenticator()
{
protected PasswordAuthentication getPasswordAuthentication()
{
return new PasswordAuthentication("*********","*******");
}
});
}
Ślad stosu jest następujący.
javax.mail.NoSuchProviderException: Invalid protocol: null
at javax.mail.Session.getProvider(Session.java:440)
at javax.mail.Session.getTransport(Session.java:659)
at javax.mail.Session.getTransport(Session.java:640)
at javax.mail.Session.getTransport(Session.java:626)
at commuSe $ConnectionHandler.sendEmail(commuSe .java:26028)
at commuSe $ConnectionHandler.run(commuSe .java:4734)
at java.lang.Thread.run(Thread.java:722)