質問

I need to send emails from my Asp.net c# site, we are hosting on ovh, here what I did:

public static void Send(string stringFrom, string stringFromPass, 
        string stringTo, string stringBody, string stringSubject, int tryNb)
    {
        // Command line argument must the the SMTP host.
        SmtpClient client = new SmtpClient("smtp.mondomaine.me", 587);
        // Specify the e-mail sender. 
        // Create a mailing address that includes a UTF8 character 
        // in the display name.
        MailAddress from = new MailAddress(stringFrom,
           "Sender",
        System.Text.Encoding.UTF8);
        // Set destinations for the e-mail message.
        MailAddress to = new MailAddress(stringTo);
        // Specify the message content.
        MailMessage message = new MailMessage(from, to);
        message.Body = stringBody;
        message.IsBodyHtml = true;
        message.BodyEncoding = System.Text.Encoding.UTF8;
        message.Subject = stringSubject;
        message.SubjectEncoding = System.Text.Encoding.UTF8;
        //provide Authentication Details need to be passed when sending email from gmail
        string password = Convert.ToBase64String(System.Text.Encoding.UTF8.GetBytes(stringFromPass));
        NetworkCredential mailAuthentication = new NetworkCredential(stringFrom, password);
        client.UseDefaultCredentials = false;
        client.Credentials = mailAuthentication; 
        // Set the method that is called back when the send operation ends.
        client.SendCompleted += new
        SendCompletedEventHandler(SendCompletedCallback);
        // The userState can be any object that allows your callback  
        // method to identify this send operation. 
        // For this example, the userToken is a string constant. 
        string userState = stringFrom + "*&(k)&*" +
            stringFromPass + "*&(k)&*" + stringTo + "*&(k)&*" +
            stringBody + "*&(k)&*" + stringSubject + "*&(k)&*" + tryNb.ToString();
        client.SendAsync(message, userState);
    }

and then i'm calling this fonction whenever i need to. I'm receiving an ovh server response:

sorry, invalid MAIL FROM for open-smtp session (http://travaux.ovh.com/?do=details&id=2602)

on this last link they told that:

messages sent via open-smtp will only be accepted if the email address of the fields From / Sender is the same connection login used for POP access.

As you see in my code i'm not using POP and i dont know how to. so why this restriction ? Any idea how to fix that will be much apreciated.

役に立ちましたか?

解決

the problem was caused by the mailAuthentication, now i'm using System.Security.SecureString instead of Convert.ToBase64String to encode passwords and everything work well.

System.Security.SecureString secureString = new System.Security.SecureString();
stringFromPass.ToCharArray().ToList().ForEach(p => secureString.AppendChar(p));
NetworkCredential mailAuthentication = new NetworkCredential(stringFrom, secureString);

In outlook, you must configure your smtp to use email and password

他のヒント

If you want to send throw Gmail-Id.


In your Email ID change the Email-setting

Follow these step

  • Click 1 --> Setting

  • Click 2 --> Forwarding and POP/IMAP

  • Clect 3 --> Enable IMAP Access


Mailing function:

MailMessage message = new MailMessage();
message.From = new MailAddress("Abc@Servername.com");
message.To.Add("Tosender@Servername.com");
message.Subject = "Your QR Code Image";
message.Body = "Please open this link and download this QR Code image for scanning your QRCode :" + QR_Code;

SmtpClient client = new SmtpClient();
client.EnableSsl = true;
client.Send(message);

In WebConfig:

<system.net>
    <mailSettings>
        <smtp deliveryMethod="Network">
            <network host="smtp.gmail.com" port="587" userName="abc@gmail.com" password="****" />
        </smtp>
    </mailSettings>
</system.net>
ライセンス: CC-BY-SA帰属
所属していません StackOverflow
scroll top