无法一次将文档中的数据发送到所有邮件ID [英] Unable to send the Data in the Document to all Mail Ids at a time

查看:70
本文介绍了无法一次将文档中的数据发送到所有邮件ID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有,其中包含学生详细信息。我需要通过邮件将一些Word文档文件发送给学生。



我成功将文件发送给所有学生,但问题是只有第一位学生收到文件和文件中的数据和剩下的学生收到文件,但数据缺失显示空白页



例如我有一些邮件ID

邮件ID列表

abc@xyz.com

def@xyz.com











venkatkumar744@gmail.com收到了文件和文件中的数据



sandeep.kazipeta@gmail.com收到了文件,但文件中的数据显示为空。





Html代码



< tr> 

< td>< asp:Label id =lbloracle11gDBArunat =serverText =Oracle 11g DBA>< / asp:Label>< / td>
< td>< asp:TextBox ID =txtoraclebatchidrunat =server
ontextchanged =txtoraclebatchid_TextChangedAutoPostBack =true>< / asp:TextBox>< / td> ;
< td>< asp:TextBox ID =txtoraclestdtrunat =server>< / asp:TextBox>< / td>
< td>< asp:TextBox ID =txtoracleendtrunat =server>< / asp:TextBox>< / td>
< td>< asp:TextBox ID =txtoracletimingsrunat =server>< / asp:TextBox>< / td>
< td>< asp:FileUpload ID =uploadrunat =server/>< / td>
< td>< asp:Button ID =btnoraclesendrunat =serverText =Sendonclick =btnoraclesend_Click/>< / td>

< / tr>









.cs文件代码



使用System.Web.UI; 
使用System.Web.UI.HtmlControls;
使用System.Web.UI.WebControls;
使用System.Web.UI.WebControls.WebParts;
使用System.Xml.Linq;
使用System.Data.SqlClient;
使用System.Net.Mail;


public partial class _Default:System.Web.UI.Page
{
protected void Page_Load(object sender,EventArgs e)
{

}


protected void btnoraclesend_Click(object sender,EventArgs e)
{


SqlConnection conn = new的SqlConnection();
conn.ConnectionString =Data Source = xxxxx; User Id = xxxxx; Password = xxxxxx; Initial Catalog = xxxxxxx; Integrated Security = SSPI;;
conn.Open();


SqlCommand cmd = new SqlCommand();
cmd.Connection = conn;
cmd.CommandText =从Student_Info中选择EMail_1,其中Branch_Code ='ap'和Student_ID in(从Admissions中选择Student_ID,其中Branch_Code ='ap'和Batch_ID ='+ txtoraclebatchid.Text +');
cmd.CommandType = CommandType.Text;

SqlDataAdapter da = new SqlDataAdapter(cmd);
da.SelectCommand = cmd;
DataSet ds = new DataSet();
da.Fill(ds);


if(ds.Tables [0] .Rows [0] [0]!= null)
{
int count = ds.Tables [0] .Rows.Count;

for(int i = 0; i< count; i ++)
{

MailMessage mm = new MailMessage();
mm.To.Add(ds.Tables [0] .Rows [i] [0] .ToString());
mm.From =新邮件地址(xxxxxxxxxx);
mm.Subject =Asp.net Test Email;


if(upload.HasFile)
{
mm.Attachments.Add(new Attachment(upload.PostedFile.InputStream,upload.FileName));
}


mm.Body =Hello world;
SmtpClient s = new SmtpClient(smtp.xxxxxxxxx.com);
s.Send(mm);
Response.Write(发送电子邮件);

}

}

conn.Close();


}
}









请告诉我解决方案.......................

解决方案

< blockquote>不太确定,看起来像这行的问题:

mm.Attachments.Add(new Attachment(upload.PostedFile.InputStream,upload.FileName));

您从流创建附件。然后流将结束。当您再次为下一个邮件创建附件时,流尚未重置为其开始位置。

我建议先将流保存到临时文件中(在循环播放学生之前)然后,从文件中创建附件。

否则,可以将流设置回原点(如果该类型的流允许这样做)。


i have a Table that contains student Details. I need to send some Word Document Files through Mails to the Students.

I Successfully sent the Documents to all the students but the problem is only the First Student received the Document and the Data in the Document and the remaining students received the Document but the Data is Missing it shows Blank page.

for Example I have some Mail Ids
list of mail Ids
abc@xyz.com
def@xyz.com
.
.
.
.

venkatkumar744@gmail.com had received the Document and the Data in the Document

sandeep.kazipeta@gmail.com had received Document but the Data in the Document shows empty.


The Html Code is

<tr>

<td><asp:Label id="lbloracle11gDBA" runat="server" Text="Oracle 11g DBA"></asp:Label></td>
<td><asp:TextBox ID="txtoraclebatchid" runat="server" 
     ontextchanged="txtoraclebatchid_TextChanged" AutoPostBack="true"></asp:TextBox></td>
<td><asp:TextBox ID="txtoraclestdt" runat="server" ></asp:TextBox></td>
<td><asp:TextBox ID="txtoracleendt" runat="server" ></asp:TextBox></td>
<td><asp:TextBox ID="txtoracletimings" runat="server" ></asp:TextBox></td>
<td><asp:FileUpload ID="upload" runat="server" /></td>
<td><asp:Button ID="btnoraclesend" runat="server" Text="Send"                 onclick="btnoraclesend_Click" /></td>

</tr>





.cs file code

using System.Web.UI;
using System.Web.UI.HtmlControls;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Xml.Linq;
using System.Data.SqlClient;
using System.Net.Mail;


public partial class _Default : System.Web.UI.Page 
{
    protected void Page_Load(object sender, EventArgs e)
    {

    }

    
    protected void btnoraclesend_Click(object sender, EventArgs e)
    {


        SqlConnection conn = new SqlConnection();
        conn.ConnectionString = "Data Source=xxxxx; User Id=xxxxx; Password=xxxxxx; Initial Catalog=xxxxxxx; Integrated Security=SSPI;";
        conn.Open();


        SqlCommand cmd = new SqlCommand();
        cmd.Connection = conn;
        cmd.CommandText = "select EMail_1 from Student_Info where Branch_Code='ap' and Student_ID in(select Student_ID from Admissions where Branch_Code='ap' and Batch_ID='" + txtoraclebatchid.Text + "')";
        cmd.CommandType = CommandType.Text;

        SqlDataAdapter da = new SqlDataAdapter(cmd);
        da.SelectCommand = cmd;
        DataSet ds = new DataSet();
        da.Fill(ds);
             

        if (ds.Tables[0].Rows[0][0] != null)
        {
            int count = ds.Tables[0].Rows.Count;

            for (int i = 0; i < count; i++)
            {
                
                MailMessage mm = new MailMessage();
                mm.To.Add(ds.Tables[0].Rows[i][0].ToString());
                mm.From = new MailAddress("xxxxxxxxxx");
                mm.Subject = "Asp.net Test Email";
                                
               
           if (upload.HasFile)
          {
              mm.Attachments.Add(new Attachment(upload.PostedFile.InputStream, upload.FileName));
          }
                      
                
                mm.Body = "Hello world";
                SmtpClient s = new SmtpClient("smtp.xxxxxxxxx.com");
                s.Send(mm);
                Response.Write("Email Sent");

            }
            
        }
                
        conn.Close();
        

    }
}





Please tell me the solution.......................

解决方案

Not really sure, looks like a problem with that line:
mm.Attachments.Add(new Attachment(upload.PostedFile.InputStream, upload.FileName));
You create an Attachment from a Stream. Afterwards the stream would be at its end. When you create the attachment for the next mail again, the stream has not been reset to its start position.
I'd suggest to save the stream to a temporary file first (before looping thro the students' list), then create the attachments from the file instead.
Otherwise, the stream could be set back to its origin (if that type of stream does allow to do so).


这篇关于无法一次将文档中的数据发送到所有邮件ID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆