SolvedPrivate

SSIS email With Script Task and C#(System.Net.Mail)

Posted on 2014-12-19
7
72 Views
Last Modified: 2016-02-15
I am attempting to send an email using the code below in a script task. After execution the package eventually errors out. The message. “System.Net.Mail.SmtpException: The operation timed out”. Is it something in the code or is there another issue. I have also tried with VB but get the same result. Thanks.

/*
   Microsoft SQL Server Integration Services Script Task
   Write scripts using Microsoft Visual C# 2008.
   The ScriptMain is the entry point class of the script.
*/

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

namespace ST_b3ce18a40f624932943eb6d8a30c09e8.csproj
{
    [System.AddIn.AddIn("ScriptMain", Version = "1.0", Publisher = "", Description = "")]
    public partial class ScriptMain : Microsoft.SqlServer.Dts.Tasks.ScriptTask.VSTARTScriptObjectModelBase
    {

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

        /*
            The execution engine calls this method when the task executes.
            To access the object model, use the Dts property. Connections, variables, events,
            and logging features are available as members of the Dts property as shown in the following examples.

            To reference a variable, call Dts.Variables["MyCaseSensitiveVariableName"].Value;
            To post a log entry, call Dts.Log("This is my log text", 999, null);
            To fire an event, call Dts.Events.FireInformation(99, "test", "hit the help message", "", 0, true);

            To use the connections collection use something like the following:
            ConnectionManager cm = Dts.Connections.Add("OLEDB");
            cm.ConnectionString = "Data Source=localhost;Initial Catalog=AdventureWorks;Provider=SQLNCLI10;Integrated Security=SSPI;Auto Translate=False;";

            Before returning from this method, set the value of Dts.TaskResult to indicate success or failure.
            
            To open Help, press F1.
      */

        public void Main()
        {
            var theMailer = new MailMessage();
            theMailer.To.Add("myname@mydomain.com");
            theMailer.From = new MailAddress("postfixemail@mydomain.com", "My Name");
            theMailer.IsBodyHtml = true;
            theMailer.Subject = "SSIS Email";
            theMailer.Body = "See Attachment";
            //*theMailer.Attachments.Add(new Attachment("C:\\LearnItFirstTest\\OurReport.xls"));
            var myClient = new SmtpClient("smtp.gmail.com", 465);
            myClient.EnableSsl = true;
            myClient.DeliveryMethod = SmtpDeliveryMethod.Network;
            myClient.UseDefaultCredentials = false;
            myClient.Credentials = new NetworkCredential("postfixemail@mydomain.com", "MyPassword");

            myClient.Send(theMailer);

            Dts.TaskResult = (int)ScriptResults.Success;
        }
    }
}
0
Comment
Question by:StampIT
  • 5
  • 2
7 Comments
 

Author Comment

by:StampIT
ID: 40510065
Additional Information. The Server the package is running on did not have Internet Information Services Active. Specifically the SMTP Server feature. This service has been started but there has been no difference in performance of the package. Not sure if the Server has to be rebooted to complete the process ??
0
 
LVL 7

Expert Comment

by:Utkarsh Kulkarni
ID: 40512897
Can you try to use default Mail sending Task in SSIS?
If not, you can check http://www.mssqltips.com/sqlservertip/1731/sending-email-from-sql-server-integration-services-ssis/ for help.
0
 

Author Comment

by:StampIT
ID: 40513012
The Send Mail Task does not allow the use of user names and passwords required for gmail. I will check out the link. Thanks.
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 

Author Comment

by:StampIT
ID: 40513363
Although I did not completely understand all that was done in Part 2 of the link I believe I am doing the same thing in the code I posted except I'm using C# and the mail settings are hard coded rather than from a SQL table. I think my issue may have more to do with the SMTP setup on the server than on the code. Any help is appreciated. Thanks.
0
 
LVL 7

Accepted Solution

by:
Utkarsh Kulkarni earned 500 total points
ID: 40514510
Just make sure of following
1. SMTP Service is installed & running
2. The server that executes this code has been allowed to use internet & SMTP.
3. Check for Firewall / Antivirus setting.
0
 

Author Comment

by:StampIT
ID: 40514909
1. Task Manager confirms that the SMTP Service is running
2. The server can get to the internet so am presuming it can use SMTP
3. No firewall or antivirus setting that would prevent internet access.

I get the error message below when I attempt to execute the package

Error: 0x1 at Step 2 - Send Mail: System.Reflection.TargetInvocationException: Exception has been thrown by the target of an invocation. ---> System.Net.Mail.SmtpException: The operation has timed out.
   at System.Net.Mail.SmtpClient.Send(MailMessage message)

Is there a way to test SMTP outside of SSIS and SQL Server that may better isolate the issue ?
0
 

Author Closing Comment

by:StampIT
ID: 40515170
The SMTP service was not running. So starting the SMTP service and using the correct port resolved the issue. Thanks.
0

Featured Post

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Ever needed a SQL 2008 Database replicated/mirrored/log shipped on another server but you can't take the downtime inflicted by initial snapshot or disconnect while T-logs are restored or mirror applied? You can use SQL Server Initialize from Backup…
If you need to start windows update installation remotely or as a scheduled task you will find this very helpful.
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

785 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question