SolvedPrivate

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

Posted on 2014-12-19
7
76 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Get 15 Days FREE Full-Featured Trial

Benefit from a mission critical IT monitoring with Monitis Premium or get it FREE for your entry level monitoring needs.
-Over 200,000 users
-More than 300,000 websites monitored
-Used in 197 countries
-Recommended by 98% of users

 

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

Enterprise Mobility and BYOD For Dummies

Like “For Dummies” books, you can read this in whatever order you choose and learn about mobility and BYOD; and how to put a competitive mobile infrastructure in place. Developed for SMBs and large enterprises alike, you will find helpful use cases, planning, and implementation.

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…
When you see single cell contains number and text, and you have to get any date out of it seems like cracking our heads.
Michael from AdRem Software outlines event notifications and Automatic Corrective Actions in network monitoring. Automatic Corrective Actions are scripts, which can automatically run upon discovery of a certain undesirable condition in your network.…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

622 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