is VB.NET SmtpClient API limited compared to Java SendMail?

你说的曾经没有我的故事 提交于 2019-12-13 03:49:50

问题


I created a method to send email in VB.NET:

Imports System.Threading
Imports System.Net.Mail
Imports System.ComponentModel

Module Module1
    Dim smtpList As New ArrayList

    Dim counter = 0

    Private Sub smtpClient_SendCompleted(ByVal sender As Object, ByVal e As AsyncCompletedEventArgs)
        System.Diagnostics.Debug.WriteLine("completed!")
        System.Console.WriteLine("completed!")
    End Sub

    Public Function createSmtp() As SmtpClient
        Dim smtp = New SmtpClient()
        Dim user = "AKAKAKAKAKAKAKAK"
        Dim host = "smtp.mailgun.org"
        Dim pass = "akakakakakakakakakakakakakakakakakakakak"

        smtp.Host = host
        smtp.Port = 587
        smtp.Credentials = New System.Net.NetworkCredential(user, pass)
        smtp.EnableSsl = True

        Return smtp
    End Function

    Public Sub SendEmail(email As String, bodystuff As String, smtp As SmtpClient)


        Dim from As New MailAddress("contat@testsitetetete.com", "Info", System.Text.Encoding.UTF8)
        Dim [to] As New MailAddress(email)
        Dim message As New MailMessage(from, [to])
        message.Body = String.Format("The message I want to send is to this <b>contact: {0}{1}</b>", vbCrLf, bodystuff)
        message.IsBodyHtml = True
        message.BodyEncoding = System.Text.Encoding.UTF8
        message.Subject = "Test email subject"
        message.SubjectEncoding = System.Text.Encoding.UTF8
        message.Priority = MailPriority.High


        ' Set the method that is called back when the send operation ends.
        AddHandler smtp.SendCompleted, AddressOf smtpClient_SendCompleted

        smtp.Send(message)
        'smtp.SendMailAsync(message)

        counter = counter + 1 ' I know its not thread safe, just to get a counter

        System.Console.WriteLine("Counter -> " & counter)

    End Sub


    Public Sub StartEmailRun()
        System.Diagnostics.Debug.WriteLine("StartEmailRun")

        'Dim smtp = createSmtp()

        Try
            For i = 0 To 8

                Dim thread As New Thread(
                  Sub()
                      Dim smtp = createSmtp()
                      SendEmail("someamail@testemail.com", "email test", createSmtp())
                  End Sub
                )
                thread.Start()
            Next

        Catch ex As Exception
            MsgBox(ex.ToString)
        End Try
    End Sub

    Sub Main()
        System.Diagnostics.Debug.WriteLine("Starting the email sending...")

        ThreadPool.SetMinThreads(100, 100) ' just to make sure no thread pool issue or limitation

        createSmtp()

        StartEmailRun()

        Thread.Sleep(300000) ' make the program alive
    End Sub

End Module

and I created this same method in java:

import java.util.Properties;

import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.Multipart;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.AddressException;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeBodyPart;
import javax.mail.internet.MimeMessage;
import javax.mail.internet.MimeMultipart;

public class TestMail 
{

    public static void main(String[] args) throws AddressException, MessagingException, InterruptedException 
    {

        Properties prop = new Properties();
        prop.put("mail.smtp.auth", true);
        prop.put("mail.smtp.starttls.enable", "true");
        prop.put("mail.smtp.host", "smtp.mailgun.org");
        prop.put("mail.smtp.port", "587");
        prop.put("mail.smtp.ssl.trust", "smtp.mailgun.org");

        Session session = Session.getInstance(prop, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                String username = "akakakakakakakakakak";
                String password = "kajdfkjasfkjasdfksjdfksdajfksdfjsdkfjdskfjkW";
                return new PasswordAuthentication(username, password);
            }
        });


        final Message message = new MimeMessage(session);
        message.setFrom(new InternetAddress("testacount@test.com"));
        message.setRecipients(
          Message.RecipientType.TO, InternetAddress.parse("atest@test.com"));
        message.setSubject("Mail Subject");

        String msg = "This is my first email using JavaMailer";

        MimeBodyPart mimeBodyPart = new MimeBodyPart();
        mimeBodyPart.setContent(msg, "text/html");

        Multipart multipart = new MimeMultipart();
        multipart.addBodyPart(mimeBodyPart);

        message.setContent(multipart);
        for (int i = 0; i < 8; i++) 
        {
            new Thread(new Runnable() {

                public void run() {
                    try {
                        Transport.send(message);
                    } catch (MessagingException e) {
                        e.printStackTrace();
                    }   
                    System.out.println("sent...");
                }
            }).start();
        }

        while(true)
        {
            Thread.sleep(1000);
        }
    }
}

I expected the VB.NET code to sent the emails simultanteously, like the java one does.

But the email sent appears to be sequencial. In the java all the emails are sent simultaneosly.

I think there is some limitation on send multiple emails in the VB.Net code.

Is there a way to send multiple emails at the same time in VB.NET???

p.s. not talking about bbc, because every email is different from another.

来源:https://stackoverflow.com/questions/55738290/is-vb-net-smtpclient-api-limited-compared-to-java-sendmail

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!