Noor Noori

Noor Noori

  • NA
  • 3
  • 523

send email through c#

Jul 8 2021 9:22 PM

I cannot run this program. it shows timeout and failure in sending mails. handshake fail due to unexpected packet format .

what to add or edit now?

using System;
using System.Data;
using Microsoft.SqlServer.Dts.Runtime;
using System.Windows.Forms;
using System.Net.Mail;          // Added
using System.Net.Mail;

namespace ST_df6618207373422d961b80ca8b6a56e2
{
    [Microsoft.SqlServer.Dts.Tasks.ScriptTask.SSISScriptTaskEntryPointAttribute]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {
        public void Main()
        {
            String SendMailFrom = Dts.Variables["EMailFrom"].Value.ToString();
            String SendMailTo = Dts.Variables["EMailTo"].Value.ToString();
            String SendMailSubject = Dts.Variables["EMailSubject"].Value.ToString();
            String SendMailBody = Dts.Variables["EMailBody"].Value.ToString();
            //User::EMailBody,User::EMailFrom,User::EMailSubject,User::EMailTo
            try
            {
                MailMessage email = new MailMessage();
                SmtpClient SmtpServer = new SmtpClient("mail.name.com");
                // START
                email.From = new MailAddress(SendMailFrom);
                email.To.Add(SendMailTo);
                email.Subject = SendMailSubject;
                email.Body = SendMailBody;
                //END

                SmtpServer.Port = 993;
               // SmtpServer.Credentials = new System.Net.NetworkCredential(SendMailFrom, "your password");
               // SmtpServer.EnableSsl = true;

                SmtpServer.Send(email);
                MessageBox.Show("Email was Successfully Sent ");
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.ToString());
            }
            // Close Script Task with success
            Dts.TaskResult = (int)ScriptResults.Success;
        }

        #region ScriptResults declaration

        enum ScriptResults
        {
            Success = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Success,
            Failure = Microsoft.SqlServer.Dts.Runtime.DTSExecResult.Failure
        };
        #endregion
    }
}

 


Answers (3)